A!leSc"pt idoraemon Nathan
! Nathanapple 2
AppleScript! 6 AppleScript! 6 AppleScript! 6 AppleScript! 6 AppleScript! 6 Automator AppleScript! 8 AppleScript! 9! 9! 9 AppleScript! 10 AppleScript! 11 1100! 11 AppleScrip! 12! 12! 12! 12! 13! 14! 16! 17! 17 AppleScript! 18 AppleScript! 18 AppleScript! 18! 20! Nathanapple 3
! 20! 20! 21! 22! 23! 24 Tell! 24 If! 24! 25 Considering/Ignoring! 26! 28! 28! 29! 29! 29! 30! 31 Try! 31 Try! 31 AppleScriptError! 31 Timeout! 32! 33 Alias! 33 POSIX! 33! 34! 34! Nathanapple 4
! 35! 35! 35! 36 run open! 36! 37! 37! 39 me! 39 script! 39 script! 39 script! 40 AppleScript! 41! 41 AppleScript! 41 Mac OS! 42! 43! Nathanapple 5
AppleScript apple apple apple apple AppleScript (Open Scripting Architecture) AppleScript (AppleScript Component) Apple (Apple Event Manager) apple apple apple apple apple apple apple apple apple apple apple apple apple! Nathanapple 6
AppleScript AppleScript apple apple apple 1 AppleScriptXcodeAppleScript Xcode GUI AppleScript Studio! Nathanapple 7
! Nathanapple 8
AppleScript 2 tell application Snow Leopard 3 2 Mac OS 10.5 10.4 AppleScript 3 AppleScript 2.3 (118)Mac OS X 10.6 2.3! Nathanapple 9
apple apple apple AppleScript apple apple apple apple apple apple apple 4 MacAdobe Microsoft! Nathanapple 10
tell application "Finder" make new folder at desktop with properties {name:"test"} repeat with a from 1 to 100 make new folder at folder "Test" of desktop with properties {name:a as string} end repeat end tell tell application "Finder" activate make new folder at folder "Desktop" of folder "Nathan" of folder "Users" of startup disk with properties {name:" "} set name of folder " " of folder "Desktop" of folder "Nathan" of folder "Users" of startup disk to "Test" make new folder at folder "Test" of folder "Desktop" of folder "Nathan" of folder "Users" of startup disk with properties {name:" "} set name of folder " " of folder "Test" of folder "Desktop" of folder "Nathan" of folder "Users" of startup disk to "1" make new folder at folder "Test" of folder "Desktop" of folder "Nathan" of folder "Users" of startup disk with properties {name:" "} set name of folder " " of folder "Test" of folder "Desktop" of folder "Nathan" of folder "Users" of startup disk to "2"!...! -- 196!... end tell 5 tell application 10.5 10.4! Nathanapple 11
AppleScrip Identifier Keyword! Boolean Number IntegerReal 6 AppleScript Language Guide developer.apple.com! Nathanapple 12
Text String Date Constant List Record firstname of {firstname:"idoraemon", lastname:"nathan"} -Class of class of "string" as -- "1.99" as real -- Real1.99Text "1.99" as integer -- Integer2 "1" as real -- Real1.0 --List "text" as list -- {"text"} 7 String Unicode TextAppleScript 2.0Text Apple AppleScript Language Guide 2008 3 text, string, and Unicode text will all compare as equal! Nathanapple 13
1.99 as list -- {1.99} {a:1, b:2} as list -- {1, 2} -- "text2" as number --Text 1 as record -- {1, 2} as record -- 8 Option option / option > 9 Java C3/2=1 AppleScript (3 as integer) / (2 as integer)1.5! Nathanapple 14
& apple apple apple! Nathanapple 15
"Text" & 1 --"Text1" (Text ) 1 & "Text" --{1, "Text"} List {name:"a"} & "b" --{name:"a", «class ktxt»:"b"} (Record) 3 & {name:"a"} --{3, "a"} (List) {1, 2, 3} & {4, 5, 6} --{1,2,3,4,5,6} List - {1, 2, 3} & 4 & 5 & 6 -- {a:1, b:2} & {c:3} --{a:1, b:2, c:3} Record - -- "Text" & {name:"a"} -- Record {name:"a"} & 3 -- IntegerRecord every character of "" characters of "A String" --{" ", " ", " ", " ", " "} --{"A", " ", "S", "t", "r", "i", "n", "g"} words of "A string" --{"A", "string"} every word of " " --{" ", " ", " ", " ", " "} characters 3 through 5 of "A String" --{"S", "t", "r"} -List words 3 through 5 of " " --{" ", " ", " "} -List word 2 of "This is a text" --"is" -Text character 3 of "This is a text" --"i" -Text ninth character of "This is a text" --"a"applescript apple apple apple apple Finder tell application "Finder" every file of desktop files of desktop --List -- every folder of desktop folders of desktop -- List -- 10! Nathanapple 16
name of every file of desktop --List end tell tell application "Finder" every file of desktop whose name begins with "a" -- a every file of desktop where its name contains "a" --a end tell End-of -line comment Block comment apple apple apple apple 11 # 2.0 AppleScript Unix! Nathanapple 17
AppleScript apple apple! Nathanapple 18
make v : Make a new element make new type : the class of the new element at location specifier : the location at which to insert the element [to specifier] : when creating an alias file, the original item to create an alias to or when creating a file viewer window, the target of the window [with properties record] : the initial values for the properties of the element specifier : to the new object(s) tell application "Finder" make new folder at desktop -- -- makenew atfolder type desktop location specifier -- make new folder at desktop with properties {name:"applescript"} --Record -- AppleScript end tell! Nathanapple 19
set name to value as type --as type set myresult to the result of (make new folder at desktop) -- tell app finder apple apple apple apple apple set message to "" -- message run anewscript displaya("") ------------------------------ ---- script anewscript -- -- displaya -- 12! Nathanapple 20
set message to "" -- message display dialog message --message end script ---- ---- on displaya(message) --displaya --message display dialog message --message end displaya ---- Record List set a to 1 set b to a display dialog " a=" & a & "; b=" & b --a 1 --ba -- a b set b to 0 --b0 display dialog "ba=" & a & "; b=" & b --a b Record List set a to {1, 2, 3, 4, 5} set b to a -- a List --balist! Nathanapple 21
display dialog " a=" & a & "; b=" & b -- a b set item 1 of b to 0 -- List b 0 display dialog "ba=" & a & "; b=" & b --a b copy set a to {1, 2, 3, 4, 5} set b to 1 copy a to b display dialog " a=" & a & "; b=" & b -- a List --copy --balist -- a b set item 1 of b to 0 display dialog "ba=" & a & "; b=" & b -- List b 0 --a b property Label : value property counttimes : 0 set counttimes to counttimes + 1 display dialog "" & counttimes & " " 13! Nathanapple 22
apple apple apple apple apple! Nathanapple 23
appletell referencetoobject to statement apple tell referencetoobjectend tell tell front window of application "Finder" to close -- tell application "Finder" close front window end tell -- appleif boolean then statement apple if boolean1 then statement1 else if boolean2 then statement2 else statement3 end if set mark to 99 --! Nathanapple 24
if mark 60 and mark < 80 then set response to "You passed the exam" else if mark 80 then set response to "Congratulations! You're smart" else set response to "Sorry. You failed" end if display dialog response repeat --do something end repeat repeat n times --do something end repeat repeat until boolean --do something end repeat repeat while boolean --do something end repeat repeat with loopvariable from startvalue to stopvalue by stepvalue --do something end repeat! Nathanapple 25
*List repeat with loopvariable in list --do something end repeat set mylist to {"Hello", "Hi", "Hey", "Goodbye"} --List -- repeat with i in mylist display dialog (contents of i) -- List set (contents of i) to "oh" -- List"oh" end repeat considering attribute1 but ignoring attribute2 --compare texts end considering 14 1.10.1 > 1.9.4 false(considering numeric strings)true! Nathanapple 26
! Nathanapple 27
Display Dialog display dialog "" display dialog " " buttons {" ", " "} default button " " with title " " with icon note giving up after 5 apple apple apple apple apple display dialog "" default answer "" 15 AppleScript Studio Xcode! Nathanapple 28
apple apple apple display alert " " message "" as warning choose from list {"", "", ""} with title "" with prompt " " default items {""} with empty selection allowed and multiple selections allowed apple apple apple apple apple apple Choose File Name choose file name with prompt " " default name "" default location file "Macintosh HD:Users"! Nathanapple 29
Choose Folder choose folder with prompt " " default location file "Macintosh HD:Users" with invisibles, multiple selections allowed and showing package contents Choose File choose file of type {"txt"}! Nathanapple 30
try --statement end try tell application "Finder" make new folder at desktop with properties {name:"applescript"} end tell display dialog "Success " try --do something at risk on error errtext number errnum -- do something to handle error end try error " " number 999 16 try! Nathanapple 31
try display dialog " AppleScript Folder " tell application "Finder" make new folder at desktop with properties {name:"applescript Folder"} end tell on error etext number enum if (enum = -48) then display dialog "\n" & etext else if (enum = -128) then display dialog "\n" & etext end if end try with timeout of x seconds --x --wait for something end timeout with timeout of 3 seconds display dialog " " end timeout display dialog " " 17 error error! Nathanapple 32
set myalias1 to alias "Macintosh HD:System:Library:CoreServices:Finder.app:" set myalias2 to alias "Macintosh HD:Users:Nathan:Desktop:example.txt" path to path to documents folder -- alias path to library folder from system domain -- alias POSIXPOSIX file POSIX path of alias "Macintosh HD:System:Library:CoreServices:Finder.app:" -- "/System/Library/CoreServices/Finder.app/" POSIX file "/Users/Nathan/Desktop/example.txt" -- file "Macintosh HD:Users:Nathan:Desktop:example.txt" 18 AliasMac OS windows 19 MacPOSIX /AppleScript! Nathanapple 33
set myfile to alias "Macintosh HD:Users:Nathan:Desktop:example.txt" read myfile set afile to alias "Macintosh HD:Users:Nathan:Desktop:example.txt" set fp to open for access afile with write permission -- write "abc" to fp -- close access fp -- 20 21! Nathanapple 34
on HelloWorld() display dialog "Hello, world" end HelloWorld -- -- -- HelloWorld() -- on Hello(somebody, howlong) display dialog somebody giving up after howlong end Hello -- -- -- Hello("Apple", 2) -- Hello("Microsoft", 1) Hello("User", 3) on Hello to somebody for howlong display dialog somebody giving up after howlong end Hello -- -- Hello to "Apple" for 2 --! Nathanapple 35
Hello for 1 to "Microsoft" -- Hello to "User" for 3 on add(x, y) set answer to (x + y) return answer end add display dialog add(1, 2) -- add run open on open names set pathnamesstring to "" -- repeat with i in names -- -- set ipath to (i as text) 22 AppleScript Language Guide! Nathanapple 36
set pathnamesstring to pathnamesstring & ipath & return end repeat -- tell application "TextEdit" set paragraph 1 of front document to pathnamesstring end tell return end open idle quit on idle beep 2 display dialog " " giving up after 1 return 5 end idle -- --5 on quit display dialog " " buttons {" ", " "} -- if button returned of result = " " then continue quit -- end if end quit! Nathanapple 37
on adding folder items to thefolder after receiving theitemlist -- display dialog ((count theitemlist) & " ) as text buttons {" ", ""} -- if button returned of result = "" then tell application "Finder" open thefolder end tell end if end adding folder items to! Nathanapple 38
property dialogtext : "ShowMe " script ShowMe display dialog dialogtext on showsomething() -- display dialog "" end showsomething end script run ShowMe showsomething() of ShowMe tell ShowMe run -- showsomething() -- end tell set thepath to ((path to desktop) as text) & "Script to Load.scpt"! Nathanapple 39
-- set thescript to load script file thepath -- run ShowMe of thescript -- showsomething() of ShowMe of thescript -- set thepath to ((path to desktop) as text) & "Script to Load.scpt" set thescript to load script file thepath run ShowMe of thescript -- set dialogtext of thescript to "" run ShowMe of thescript -- store script thescript in file thepath with replacing! Nathanapple 40
AppleScript about above after against and apart from around as aside from at back before beginning behind below beneath beside between but by considering contain contains contains continue copy div does eighth else end equal equals error every exit false fifth first for fourth from front get given global if ignoring in instead of into is it its last local me middle mod my ninth not of on onto or out of over prop property put ref reference repeat return returning script second set seventh since sixth some tell tenth that the then third through thru timeout times to transaction true try until where while whose with without! Nathanapple 41
! Nathanapple 42
! Nathanapple 43