applescript编辑软件的使用
1、让计算机发声。最后发两次“咚”的声音
- using为使用哪种朗读的语音,省略using关键字,就是用默认的朗读语音。
- beep发出咚咚的声音 beep number
say "Let's study applescript" using "Fred"
beep 2
2、调用Finder程序,去清空回收站里面的垃圾
tell application "Finder"
empty the trash
end tell
3、变量赋值
set str to ""
set str2 to "test"
set result to "Nealyang"
4、弹框提示消息
- display dialog显示弹框
set stringToBeDispalyed to "hi there"
display dialog "stringToBeDispalyed"
display dialog stringToBeDispalyed
5、字符串拼接
- 使用&拼接字符串
set nameOfActress to "Neal is "
set actressRating to " very pretty"
set resultingString to nameOfActress & actressRating
display dialog resultingString
6、类型转换
set strToNumber to "16" as number
set numToStr to 12 as string
7、获取字符串长度
set theLength to the length of "Neal"
8、弱类型语言数组赋值
set exampleList to {1, 2, 3, "hahah", 9}
set addToBegin to {"winter"}
set addToEnd to {"summer", "autumn"}
set currentList to {"spring"}
set modifiedList to addToBegin & currentList & addToEnd
9、取值
- 最左边的index是1
set testList to {"Neal", "haha"}
set item 2 of testList to "yang"
get testList
set the second item of testList to "yang"
set the 2nd item of testList to "yang"
- 获取最后一个值
set myList to {"neal", "haha"}
set valueOfLastItem to item -1 of myList
set myList to {"a", "b", "c", "d", "e", "f"}
set shortList to items 2 through 5 of myList
10、reverse取反
set reversedList to reverse of {2, 3, 4, 6, 7}
11、获取数组长度
- the length of 和 the count of 效果是一样的
set theListLength to the length of {"ds", 1, 2, 3}
set theListLength to the count of {"ds", 1, 2, 3}
12、随机取值
- 随机返回列表中的任一元素
set x to some item of {1, 2, 3, 4, 5, 7, 7, 6, 5}
13、条件语句
set ageEntered to 73
set myAge to 24
if ageEntered is myAge then
display dialog "You are as old as I am "
beep
end if
say "this sentence is spoken anyway"
14、路径、文件夹和应用程序
set thePath to alias "Macintosh HD:Users:zhouwei:Documents:google"
tell application "Finder"
open folder thePath
end tell
- 列出所选文件夹中所有的文件夹名称
#set thePath to alias "Macintosh HD:Users:xx:Documents:google"
set folderSelected to choose folder "Select a folder"
tell application "Finder"
set listOfFolders to every folder of folderSelected
end tell
set theList to {}
repeat with aFolder in listOfFolders
set temp to the name of aFolder
set theList to theList & temp
end repeat
15、带返回值的调用
(*带返回值的函数调用*)
on circleArea(radius)
set area to pi * (radius ^ 2)
return area
end circleArea
set anArea to circleArea(10)
16、完整发送邮件
(*发送邮件*)
set recipientName to "小红"
set recipientAddress to "[email protected]"
set theSubject to "AppleScript Automated Email"
set theContent to "This email was created and sent using AppleScript!"
tell application "Mail"
--创建消息
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
--设置接收信息
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
--发送邮件
send
end tell
end tell
17、读写文件
set myFile to alias "Macintosh HD:Users:xiaxuqiang:Desktop:example.txt"
read myFile
set aFile to alias "Macintosh HD:Users:xiaxuqiang:Desktop:example.txt"
set fp to open for access aFile with write permission
write "AppleScript写入文本" to fp
close access fp
18、打印提示
- javascript
tell application "Finder"
tell me to log "My log entry."
end tell
- javascript
console.log("dd");
19、OC桥接大小写转换
use framework "Foundation"
on changeCaseOfText(theText, theCase)
set theText to stringWithString_(theText) of NSString of current application
if theCase contains "lower" then
set theNewText to lowercaseString() of theText
else if theCase contains "upper" then
set theNewText to uppercaseString() of theText
else
set theNewText to capitalizedString() of theText
end if
return (theNewText as string)
end changeCaseOfText
changeCaseOfText("hello", "upper")
20、创建plist文件
(*创建plist文件*)
tell application "System Events"
set theParentDictionary to make new property list item with properties {kind:record}
set thePropertyListFilePath to "/Users/zhouwei/Desktop/applescript/Example.plist"
set thePropertyListFile to make new property list file with properties {contents:theParentDictionary, name:thePropertyListFilePath}
-- 第一种方式
tell property list items of thePropertyListFile
--添加boolean key
make new property list item at end with properties {kind:boolean, name:"booleanKey", value:true}
--添加date key
make new property list item at end with properties {kind:date, name:"dateKey", value:current date}
--添加list key
make new property list item at end with properties {kind:list, name:"listKey"}
--添加number key
make new property list item at end with properties {kind:number, name:"numberKey", value:5}
--添加record/dictionary key
make new property list item at end with properties {kind:record, name:"recordKey"}
--添加string key
make new property list item at end with properties {kind:string, name:"stringKey", value:"string value"}
end tell
-- 第二种方式
tell property list file thePropertyListFilePath
-- 更新值
set value of property list item "stringKey" to "new string value"
-- 添加值
make new property list item at end with properties {kind:string, name:"stringKey2", value:"string value"}
end tell
end tell