Apple Script 常用函数

1.读取文件内容返回列表

-- 定义函数,参数为文件路径
on readLinesFromFile(filePath)
	try
		set fileHandler to open for access filePath
		set fileContents to read fileHandler as «class utf8»
		close access fileHandler
		set linesArray to paragraphs of fileContents
		
		return linesArray
	on error errMsg
		try
			close access filePath
		end try
		display dialog "发生错误:" & errMsg buttons {"OK"} default button "OK"
		return {}
	end try
end readLinesFromFile

2.解析json字符串为对象

set JSONString to "[{  \"name\": \"AppleScript\", \"type\": \"Scripting Language\",\"see also\": [ \"Hypercard\"] },{ \"name\": \"JavaScript\",\"type\": \"Prototype Based\", \"see also\": [\"ECMAScript\", \"Python\"] } ]"
set applescriptValue to run script (do shell script ("echo " & JSONString's quoted form & " | sed -E 's/\"([^\"]+)\"[[:space:]]*:[[:space:]]*/|\\1|:/g' | tr -d '\\n\\r'"))
log applescriptValue

3.Apple Script 发送get数据

on HttpGET(getUrl, getData, headers)
	set curlCommand to "curl -X GET"
	set isJsonRequest to false
	if (count of headers) is not 0 then
		repeat with myRecord in headers
			set curlCommand to curlCommand & " -H " & quoted form of (name of myRecord & ": " & value of myRecord)
			if name of myRecord is "Content-Type" and value of myRecord is "application/json" then
				set isJsonRequest to true
			end if
		end repeat
	end if
	if (count of getData) is not 0 then
		set getDataString to ""
		repeat with myRecord in getData
			if isJsonRequest then
				set getDataString to getDataString & "\"" & name of myRecord & "\":\"" & value of myRecord & "\","
			else
				set getDataString to getDataString & name of myRecord & "=" & value of myRecord & "&"
			end if
		end repeat
		if isJsonRequest then
			set getDataString to "{" & text 1 thru -2 of getDataString & "}"
			set curlCommand to curlCommand & " --data-raw " & quoted form of getDataString
			
		else
			set curlCommand to curlCommand & " -d " & quoted form of text 1 thru -2 of getDataString -- 移除末尾的多余的 "&"
		end if
	end if
	set curlCommand to curlCommand & " " & quoted form of getUrl
	set response to do shell script curlCommand
	if isJsonRequest then
		return run script (do shell script ("echo " & response's quoted form & " | sed -E 's/\"([^\"]+)\"[[:space:]]*:[[:space:]]*/|\\1|:/g' | tr -d '\\n\\r'"))
	else
		return response
	end if
end HttpGET

3.Apple Script 发送psot数据

on httpPost(postUrl, postData, headers)
	set curlCommand to "curl -X POST"
	set isJsonRequest to false
	if (count of headers) is not 0 then
		repeat with myRecord in headers
			set curlCommand to curlCommand & " -H " & quoted form of (name of myRecord & ": " & value of myRecord)
			if name of myRecord is "Content-Type" and value of myRecord is "application/json" then
				set isJsonRequest to true
			end if
		end repeat
	end if
	if (count of postData) is not 0 then
		set postDataString to ""
		repeat with myRecord in postData
			if isJsonRequest then
				set postDataString to postDataString & "\"" & name of myRecord & "\":\"" & value of myRecord & "\","
			else
				set postDataString to postDataString & name of myRecord & "=" & value of myRecord & "&"
			end if
		end repeat
		if isJsonRequest then
			set postDataString to "{" & text 1 thru -2 of postDataString & "}"
			set curlCommand to curlCommand & " --data-raw " & quoted form of postDataString
		else
			set curlCommand to curlCommand & " -d " & quoted form of text 1 thru -2 of postDataString -- 移除末尾的多余的 "&"
		end if
	end if
	set curlCommand to curlCommand & " " & quoted form of postUrl
	set response to do shell script curlCommand
	
	if isJsonRequest then
		return run script (do shell script ("echo " & response's quoted form & " | sed -E 's/\"([^\"]+)\"[[:space:]]*:[[:space:]]*/|\\1|:/g' | tr -d '\\n\\r'"))
	else
		return response
	end if
end httpPost
This entry was posted in apple script. Bookmark the permalink.

发表回复