applescript book

发布时间 2023-04-10 19:07:16作者: ploolq

Apple Automator 2009
with AppleScript Bible

set randNumber to (random number (20)) as integer

if (randNumber > 10) then
	display dialog randNumber
end if
set theTime to "now"

if (theTime = "now") then
	display dialog "Fly!"
end if
set randNumber to (random number (20)) as integer
if (randNumber > 10) then
	display dialog "More than 10: " & randNumber
else
	display dialog "10 or less: " & randNumber
end if
set randNumber to (random number (20)) as integer
if (randNumber > 10) then
	display dialog "More than 10: " & randNumber
else if (randNumber ≥ 7 and randNumber ≤ 9) then
	display dialog "Between 7 & 9: " & randNumber
else if (randNumber ≥ 3 and randNumber < 7) then
	display dialog "Between 3 and 6: " & randNumber
else
	display dialog "It’s a 0, 1, or 2: " & randNumber
end if
repeat
	display dialog ¬
		"Please enter your first name:" default answer ""

	set firstName to text returned of result

	if firstName is not equal to "" then exit repeat

end repeat
repeat 3 times
	display dialog ¬
		"Please enter your first name:" default answer ""

	set firstName to text returned of result

	if firstName is not equal to "" then exit repeat
end repeat

if firstName = "" then display dialog ¬
	"You never gave us your first name!"
tell application "Finder"
	repeat with incrementValue from 1 to 3
		make new folder at desktop with properties ¬
			{name:incrementValue as string}
	end repeat
end tell
tell application "Finder"
	repeat with incrementValue from 1 to 50 by 5
		make new folder at desktop with properties ¬
			{name:incrementValue as string}
	end repeat
end tell
tell application "Finder"
	repeat with incrementValue from 10 to 1 by -1
		make new folder at desktop with properties ¬
			{name:incrementValue as string}
	end repeat
end tell
set loopCounter to 1
repeat until loopCounter = 30
	display dialog loopCounter
	set loopCounter to loopCounter + 1
end repeat
set firstName to ""
repeat until firstName is not equal to ""
	display dialog ¬
		"Please enter your first name:" default answer ""
	set firstName to text returned of result
end repeat
set myLogFile to (path to desktop as string) & "test.log"

tell application "Finder"
	repeat while (file myLogFile exists) = false
	end repeat
end tell

display dialog "The log file is on the desktop!"
set myList to {"dogs", "cats", "cars", "planes", "food"}

tell application "Finder"
	repeat with noun in myList
		make new folder at desktop with properties ¬
			{name:noun as string}
	end repeat
end tell
set destFolder to (path to current user folder)

display dialog "New Folder Name" default answer ""
set newFolder to the text returned of result

tell application "Finder"
	if (exists folder newFolder of destFolder) then
		display dialog "Sorry, that folder exists!"
	else
		make new folder at destFolder ¬
			with properties {name:newFolder}
		display dialog "New folder created!"
	end if
end tell
repeat with myvol from 1 to 10
    set volume myvol
    beep
    delay 1
end repeat
display dialog ¬
    "AppleScript rocks!" buttons {"Meh", "Right on!"} ¬
        default button "Right on!"

display dialog ¬
    "AppleScript rocks!" buttons {"Meh", "Right on!"} ¬
        default button 2

display dialog "what is your favorite color?" ¬
    default answer "blue"
display dialog ¬
	"what is your favorite color?" buttons {"blue", "red", "yellow"} ¬
	default button "blue"

set myColor to button returned of result
display dialog myColor
choose folder with prompt ¬
    "Choose a folder:" ¬
        default location (path to desktop folder)


choose folder with prompt ¬
    "Choose a folder:" ¬
        default location (path to desktop folder) ¬
    with multiple selections allowed
set myFiles to ¬
    choose file with prompt ¬
        "Choose file(s):" default location (path to documents folder) ¬
    with multiple selections allowed

set myList to {"red", "blue", "yellow", "green", "orange", "purple"}
choose from list myList


set myList to {"red", "blue", "yellow", "green", "orange", "purple"}
choose from list myList
set myColor to item 1 of result


set myList to {"red", "blue", "yellow", "green", "orange", "purple"}
choose from list myList ¬
    with prompt ¬
        "Favorite color?" default items {"blue"} ¬
    with multiple selections allowed
    set myColor to result
choose application with prompt ¬
    "Launch application(s)" ¬
    with multiple selections allowed
on addTwo(x, y)
    return x + y
end addTwo
display dialog addTwo(20,50)


set myListCount to count of {"eggs", "milk", "bread"}
set myRandom to (random number 100)

display dialog addTwo(myListCount, myRandom)
on addTwo(x, y)
    return x + y
end addTwo
on factorial(x)
    if x > 0 then
        return x * (factorial(x - 1))
    else
        return 1
    end if
end factorial

display dialog factorial(3)
set mathlib to (load script file ¬
"myerman:library:scripts:myerman:mathlib.scpt")

tell mathlib
    factorial(10)
end tell
on createFolder(newFolder, ExistingFolder)
    tell application "Finder"
        make new folder at folder ExistingFolder ¬
            with properties {name:newFolder}
    end tell
end createFolder
set myFolder to text returned of ¬
    (display dialog ¬
        "Choose new folder name" default answer "test")

set myDesktop to choose folder with prompt ¬
"Save folder in location" ¬
default location (path to desktop folder)
createFolder(myFolder, myDesktop)

on createFolder(newFolder, ExistingFolder)
    tell application "Finder"
        make new folder at folder ExistingFolder ¬
        with properties {name:newFolder}
    end tell
end createFolder

on trashIt(itemPath)
tell application "Finder"
delete item itemPath
end tell
end trashIt

set myDesktop to ¬
    (choose folder with prompt ¬
    "Choose a folder" default location (path to desktop folder))

set counter to countFolderItems(myDesktop)
display dialog counter

on countFolderItems(folderName)
    tell application "Finder"
        set theFolder to (folder folderName)
        set myCount to the count of (every item of theFolder)
        return myCount
    end tell
end countFolderItems
set myFile to choose file
set mySize to ¬
    size of (info for myFile) as string
display dialog (myFile as string) & ¬
return & "size: " & mySize
set myFiles to ¬
choose file with multiple selections allowed
getSizes(myFiles)

on getSizes(files_)
    set myString to ""
    repeat with file_ in files_
        set myFile to ((file_ as string) & return ¬
            & "size: " & size of (info for file_) ¬
            as string) & return & return
        set myString to myString & myFile
    end repeat
display dialog myString
end getSizes

https://files.cnblogs.com/files/Searchor/AppleAutomatorwithAppleScriptBible.zip

tell application "Finder"
    try
        set desktop picture to file (file_ as text)
    on error
        display dialog "Invalid file!" with icon 0
    end try
end tell