Add more applescript samples

This commit is contained in:
Joshua Peek
2012-08-06 10:22:38 -05:00
parent a13f246e4f
commit a35fa88f50
7 changed files with 670 additions and 35 deletions

View File

@@ -257,8 +257,8 @@
".gemrc"
]
},
"tokens_total": 331324,
"languages_total": 254,
"tokens_total": 333026,
"languages_total": 260,
"tokens": {
"Apex": {
"/*": 15,
@@ -656,56 +656,249 @@
"TwilioAPI.getDefaultAccount": 1
},
"AppleScript": {
"set": 13,
"set": 108,
"windowWidth": 3,
"to": 13,
"to": 128,
"windowHeight": 3,
"delay": 1,
"AppleScript": 1,
"s": 1,
"text": 1,
"item": 2,
"delay": 3,
"AppleScript": 2,
"s": 3,
"text": 13,
"item": 13,
"delimiters": 1,
"tell": 14,
"application": 5,
"tell": 40,
"application": 16,
"screen_width": 2,
"(": 7,
"do": 2,
"(": 89,
"do": 4,
"JavaScript": 2,
"in": 2,
"in": 13,
"document": 2,
")": 7,
")": 88,
"screen_height": 2,
"end": 9,
"end": 67,
"myFrontMost": 3,
"name": 1,
"of": 9,
"name": 8,
"of": 72,
"first": 1,
"processes": 1,
"processes": 2,
"whose": 1,
"frontmost": 1,
"is": 1,
"true": 1,
"{": 11,
"is": 40,
"true": 8,
"{": 32,
"desktopTop": 2,
"desktopLeft": 1,
"desktopRight": 1,
"desktopBottom": 1,
"}": 11,
"bounds": 1,
"}": 32,
"bounds": 2,
"desktop": 1,
"try": 4,
"process": 2,
"try": 10,
"process": 5,
"w": 5,
"h": 4,
"size": 3,
"size": 5,
"drawer": 2,
"window": 4,
"on": 2,
"error": 2,
"window": 5,
"on": 18,
"error": 3,
"position": 1,
"-": 4,
"/": 2
"-": 57,
"/": 2,
"(*": 8,
"*)": 8,
"property": 7,
"type_list": 6,
"extension_list": 6,
"html": 2,
"not": 5,
"currently": 2,
"handled": 2,
"run": 4,
"FinderSelection": 4,
"the": 56,
"selection": 2,
"as": 27,
"alias": 8,
"list": 9,
"FS": 10,
"Ideally": 2,
"this": 2,
"could": 2,
"be": 2,
"passed": 2,
"open": 8,
"handler": 2,
"SelectionCount": 6,
"number": 6,
"count": 10,
"if": 50,
"then": 28,
"userPicksFolder": 6,
"else": 14,
"MyPath": 4,
"path": 6,
"me": 2,
"If": 2,
"I": 2,
"m": 2,
"a": 4,
"double": 2,
"clicked": 2,
"droplet": 2,
"these_items": 18,
"choose": 2,
"file": 6,
"with": 11,
"prompt": 2,
"type": 6,
"thesefiles": 2,
"item_info": 24,
"repeat": 19,
"i": 10,
"from": 9,
"this_item": 14,
"info": 4,
"for": 5,
"folder": 10,
"processFolder": 8,
"false": 9,
"and": 7,
"or": 6,
"extension": 4,
"theFilePath": 8,
"string": 17,
"thePOSIXFilePath": 8,
"POSIX": 4,
"processFile": 8,
"folders": 2,
"theFolder": 6,
"without": 2,
"invisibles": 2,
"&": 63,
"thePOSIXFileName": 6,
"terminalCommand": 6,
"convertCommand": 4,
"newFileName": 4,
"shell": 2,
"script": 2,
"need": 1,
"pass": 1,
"URL": 1,
"Terminal": 1,
"localMailboxes": 3,
"every": 3,
"mailbox": 2,
"greater": 5,
"than": 6,
"messageCountDisplay": 5,
"return": 16,
"my": 3,
"getMessageCountsForMailboxes": 4,
"everyAccount": 2,
"account": 1,
"eachAccount": 3,
"accountMailboxes": 3,
"outputMessage": 2,
"make": 3,
"new": 2,
"outgoing": 2,
"message": 2,
"properties": 2,
"content": 2,
"subject": 1,
"visible": 2,
"font": 2,
"theMailboxes": 2,
"mailboxes": 1,
"returns": 2,
"displayString": 4,
"eachMailbox": 4,
"mailboxName": 2,
"messageCount": 2,
"messages": 1,
"unreadCount": 2,
"unread": 1,
"padString": 3,
"theString": 4,
"fieldLength": 5,
"integer": 3,
"stringLength": 4,
"length": 1,
"paddedString": 5,
"character": 2,
"less": 1,
"equal": 3,
"paddingLength": 2,
"times": 1,
"space": 1,
"lowFontSize": 9,
"highFontSize": 6,
"messageText": 4,
"userInput": 4,
"display": 4,
"dialog": 4,
"default": 4,
"answer": 3,
"buttons": 3,
"button": 4,
"returned": 5,
"minimumFontSize": 4,
"newFontSize": 6,
"result": 2,
"theText": 3,
"exit": 1,
"fontList": 2,
"activate": 3,
"crazyTextMessage": 2,
"eachCharacter": 4,
"characters": 1,
"some": 1,
"random": 4,
"color": 1,
"current": 3,
"pane": 4,
"UI": 1,
"elements": 1,
"enabled": 2,
"tab": 1,
"group": 1,
"click": 1,
"radio": 1,
"get": 1,
"value": 1,
"field": 1,
"isVoiceOverRunning": 3,
"isRunning": 3,
"contains": 1,
"isVoiceOverRunningWithAppleScript": 3,
"isRunningWithAppleScript": 3,
"VoiceOver": 1,
"x": 1,
"vo": 1,
"cursor": 1,
"currentDate": 3,
"date": 1,
"amPM": 4,
"currentHour": 9,
"minutes": 2,
"<": 2,
"below": 1,
"sound": 1,
"nice": 1,
"currentMinutes": 4,
"ensure": 1,
"nn": 2,
"gets": 1,
"AM": 1,
"readjust": 1,
"hour": 1,
"time": 1,
"currentTime": 3,
"day": 1,
"output": 1,
"say": 1
},
"Arduino": {
"void": 2,
@@ -26464,7 +26657,7 @@
},
"language_tokens": {
"Apex": 4401,
"AppleScript": 176,
"AppleScript": 1878,
"Arduino": 20,
"AutoHotkey": 3,
"C": 29074,
@@ -26534,7 +26727,7 @@
},
"languages": {
"Apex": 6,
"AppleScript": 1,
"AppleScript": 7,
"Arduino": 1,
"AutoHotkey": 1,
"C": 18,
@@ -26602,5 +26795,5 @@
"XSLT": 1,
"YAML": 1
},
"md5": "fe2b81cafdbca2f11e0d7439b1437c35"
"md5": "e5daa0ff6e34a82348f754c1584d0040"
}

View File

@@ -0,0 +1,87 @@
(*
Copyright 2003 Apple Computer, Inc.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
property type_list : {"JPEG", "GIFf", "PICT", "TIFF", "PDF", "TEXT"}
property extension_list : {"jpg", "gif", "pct", "tif", "pdf", "rtf"}
--html is not currently handled
on run {}
tell application "Finder" to set FinderSelection to the selection as alias list
set FS to FinderSelection
--Ideally, this list could be passed to the open handler
set SelectionCount to number of FS -- count
if SelectionCount is 0 then
set FS to userPicksFolder()
else if the SelectionCount is 1 then
set MyPath to path to me
if MyPath is item 1 of FS then
--If I'm a droplet then I was double-clicked
set FS to userPicksFolder()
end if
else
--I'm not a double-clicked droplet
end if
open FS
end run
on userPicksFolder()
set these_items to {}
set these_items to (choose file with prompt "Select a file to convert to PDF:" of type {"JPEG", "GIFf", "PICT", "TIFF", "TEXT", "RTF"}) as list
end userPicksFolder
on open these_items
set thesefiles to {}
set the item_info to {}
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items)
set the item_info to info for this_item
if folder of the item_info is true then --if the item is a folder
processFolder(this_item)
else if ((folder of the item_info is false) and (alias of the item_info is false)) and (the file type of the item_info is in the type_list) or ((the name extension of the item_info) is in the extension_list) then
set theFilePath to (item i of these_items as string)
set thePOSIXFilePath to POSIX path of theFilePath as string
processFile(thePOSIXFilePath)
end if
end repeat
end open
--process folders
on processFolder(theFolder)
set these_items to list folder theFolder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((theFolder as text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
processFolder(this_item)
else if (alias of the item_info is false) and ((the file type of the item_info is in the type_list) or the name extension of the item_info is in the extension_list) then
set theFilePath to (this_item as string)
set thePOSIXFilePath to POSIX path of theFilePath as string
processFile(thePOSIXFilePath)
end if
end repeat
end processFolder
on processFile(thePOSIXFileName)
try
set terminalCommand to ""
set convertCommand to "/System/Library/Printers/Libraries/./convert "
set newFileName to thePOSIXFileName & ".pdf"
set terminalCommand to convertCommand & "-f " & "\"" & thePOSIXFileName & "\"" & " -o " & "\"" & newFileName & "\"" & " -j \"application/pdf\""
do shell script terminalCommand
end try
end processFile

View File

@@ -0,0 +1,91 @@
(*
Copyright 2003 Apple Computer, Inc.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
property type_list : {"JPEG", "GIFf", "PICT", "TIFF", "PDF", "TEXT"}
property extension_list : {"jpg", "gif", "pct", "tif", "pdf", "rtf"}
--html is not currently handled
on run {}
tell application "Finder" to set FinderSelection to the selection as alias list
set FS to FinderSelection
--Ideally, this list could be passed to the open handler
set SelectionCount to number of FS -- count
if SelectionCount is 0 then
set FS to userPicksFolder()
else if the SelectionCount is 1 then
set MyPath to path to me
if MyPath is item 1 of FS then
--If I'm a droplet then I was double-clicked
set FS to userPicksFolder()
end if
else
--I'm not a double-clicked droplet
end if
open FS
end run
on userPicksFolder()
set these_items to {}
set these_items to (choose file with prompt "Select a file to convert to PostScript:" of type {"JPEG", "GIFf", "PICT", "TIFF", "TEXT", "RTF"}) as list
end userPicksFolder
on open these_items
set thesefiles to {}
set the item_info to {}
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items)
set the item_info to info for this_item
if folder of the item_info is true then --if the item is a folder
processFolder(this_item)
else if ((folder of the item_info is false) and (alias of the item_info is false)) and (the file type of the item_info is in the type_list) or ((the name extension of the item_info) is in the extension_list) then
set theFilePath to (item i of these_items as string)
set thePOSIXFilePath to POSIX path of theFilePath as string
processFile(thePOSIXFilePath)
end if
end repeat
end open
--process folders
on processFolder(theFolder)
set these_items to list folder theFolder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((theFolder as text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
processFolder(this_item)
else if (alias of the item_info is false) and ((the file type of the item_info is in the type_list) or the name extension of the item_info is in the extension_list) then
set theFilePath to (this_item as string)
set thePOSIXFilePath to POSIX path of theFilePath as string
processFile(thePOSIXFilePath)
end if
end repeat
end processFolder
--need to pass the URL to Terminal
on processFile(thePOSIXFileName)
try
set terminalCommand to ""
set convertCommand to "/System/Library/Printers/Libraries/./convert "
set newFileName to thePOSIXFileName & ".ps"
set terminalCommand to convertCommand & "-f " & "\"" & thePOSIXFileName & "\"" & " -o " & "\"" & newFileName & "\"" & " -j \"application/postscript\""
do shell script terminalCommand
end try
end processFile

View File

@@ -0,0 +1,80 @@
(*
Count Messages in All Mailboxes
Copyright 2002-2012 Apple Inc. All rights reserved.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
(*
This script goes through each mailbox, gets the total message count and
the unread count, then displays the final output in a new email message.
*)
tell application "Mail"
set localMailboxes to every mailbox
if (count of localMailboxes) is greater than 0 then
set messageCountDisplay to "Local mailboxes (On My Mac)" & return & my getMessageCountsForMailboxes(localMailboxes)
else
set messageCountDisplay to ""
end if
set everyAccount to every account
repeat with eachAccount in everyAccount
set accountMailboxes to every mailbox of eachAccount
if (count of accountMailboxes) is greater than 0 then
set messageCountDisplay to messageCountDisplay & return & "Mailboxes for Account: " & name of eachAccount & return & my getMessageCountsForMailboxes(accountMailboxes)
end if
end repeat
set outputMessage to make new outgoing message with properties {content:messageCountDisplay, subject:"Message counts for all my mailboxes", visible:true}
tell outputMessage
set font to "Courier"
set size to 12
end tell
end tell
on getMessageCountsForMailboxes(theMailboxes)
-- (list of mailboxes)
-- returns string
set displayString to ""
tell application "Mail"
repeat with eachMailbox in theMailboxes
set mailboxName to name of eachMailbox
set messageCount to (count of (messages of eachMailbox)) as string
set unreadCount to unread count of eachMailbox as string
set displayString to displayString & " " & my padString(mailboxName, 40) & " " & messageCount & " (" & unreadCount & " unread)" & return
end repeat
end tell
return displayString
end getMessageCountsForMailboxes
on padString(theString, fieldLength)
-- (string, integer)
-- returns string
set stringLength to length of theString
if stringLength is greater than fieldLength then
set paddedString to (text from character 1 to character (fieldLength - 3) of theString) & "..."
else -- stringLength is less than or equal to fieldLength
set paddedString to theString
set paddingLength to fieldLength - stringLength
repeat paddingLength times
set paddedString to paddedString & space
end repeat
end if
return paddedString
end padString

View File

@@ -0,0 +1,68 @@
(*
Crazy Message Text
Copyright 2002-2012 Apple Inc. All rights reserved.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
(*
This script takes a string from the user and then makes a new message
where each letter has a different font, size, and color.
*)
property lowFontSize : 36
property highFontSize : 72
property messageText : "Happy Birthday!"
repeat
set userInput to display dialog "Enter some message text:" & return & return & "Minimum Character Size: " & (lowFontSize as string) & return & "Maximum Character Size: " & (highFontSize as string) default answer messageText buttons {"Cancel", "Set Prefs", "Continue"} default button 3
if the button returned of userInput is "Set Prefs" then
set minimumFontSize to 9
display dialog "Enter the minimum font size to use:" & return & return & "(Must be at least " & (minimumFontSize as string) & ")" default answer lowFontSize buttons {"OK"}
set newFontSize to text returned of the result as integer
if newFontSize is greater than or equal to minimumFontSize then
set lowFontSize to newFontSize
else
set lowFontSize to minimumFontSize
end if
display dialog "Enter the maximum font size to use:" & return & return & "(Must be greater than " & (lowFontSize as string) & ")" default answer highFontSize buttons {"OK"}
set newFontSize to text returned of the result as integer
if newFontSize is greater than lowFontSize then
set highFontSize to newFontSize
else
set highFontSize to lowFontSize
end if
else -- button returned of userInput is "Continue"
set theText to text returned of userInput
if theText is not "" then
set messageText to theText
end if
exit repeat
end if
end repeat
set fontList to {"American Typewriter", "American Typewriter Light", "American Typewriter Bold", "American Typewriter Condensed", "American Typewriter Condensed Light", "American Typewriter Condensed Bold", "Arial", "Arial Italic", "Arial Bold", "Arial Bold Italic", "Arial Black", "Baskerville", "Baskerville Italic", "Baskerville SemiBold", "Baskerville Bold", "Baskerville SemiBold Italic", "Baskerville Bold Italic", "Big Caslon Medium", "Comic Sans MS", "Comic Sans MS Bold", "Copperplate", "Copperplate Light", "Copperplate Bold", "Didot", "Didot Italic", "Didot Bold", "Futura Medium", "Futura Medium Italic", "Futura Condensed Medium", "Futura Condensed ExtraBold", "Geneva", "Gill Sans", "Gill Sans Italic", "Gill Sans Light", "Gill Sans Light Italic", "Gill Sans Bold", "Gill Sans Bold Italic", "Herculanum", "Lucida Grande", "Lucida Grande Bold", "Marker Felt Thin", "Marker Felt Wide", "Optima Regular", "Optima Italic", "Optima Bold", "Optima Bold Italic", "Optima ExtraBlack", "Papyrus", "Verdana", "Verdana Italic", "Verdana Bold", "Verdana Bold Italic", "Zapfino"}
tell application "Mail"
activate
set crazyTextMessage to make new outgoing message with properties {content:messageText, visible:true}
tell crazyTextMessage
repeat with eachCharacter in characters
set font of eachCharacter to (some item of fontList)
set size of eachCharacter to (random number from lowFontSize to highFontSize)
set color of eachCharacter to {random number from 0 to 65535, random number from 0 to 65535, random number from 0 to 65535}
end repeat
end tell
end tell

View File

@@ -0,0 +1,41 @@
(*
Get User Name
This script uses UI element scripting to get the name for the
current user.
If "Enable access for assistive devices" is not checked,
this script will open the Universal Access System Preference and ask
the user to check the checkbox.
Copyright 2007 Apple Inc.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
tell application "System Preferences"
activate
set current pane to pane "com.apple.preferences.users"
end tell
tell application "System Events"
if UI elements enabled then
tell tab group 1 of window "Accounts" of process "System Preferences"
click radio button 1
delay 2
get value of text field 1
end tell
else
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\""
end tell
end if
end tell

View File

@@ -0,0 +1,75 @@
(*
Speaks the date and time of day
Copyright 2008 Apple Inc. All rights reserved.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
on isVoiceOverRunning()
set isRunning to false
tell application "System Events"
set isRunning to (name of processes) contains "VoiceOver"
end tell
return isRunning
end isVoiceOverRunning
on isVoiceOverRunningWithAppleScript()
if isVoiceOverRunning() then
set isRunningWithAppleScript to true
-- is AppleScript enabled on VoiceOver --
tell application "VoiceOver"
try
set x to bounds of vo cursor
on error
set isRunningWithAppleScript to false
end try
end tell
return isRunningWithAppleScript
end if
return false
end isVoiceOverRunningWithAppleScript
set currentDate to current date
set amPM to "AM"
set currentHour to (currentDate's hours)
set currentMinutes to currentDate's minutes
if (currentHour > 12 and currentHour < 24) then
set amPM to "PM"
else
set amPM to "AM"
end if
-- make minutes below 10 sound nice
if currentMinutes < 10 then
set currentMinutes to ("0" & currentMinutes) as text
end if
-- ensure 0:nn gets set to 12:nn AM
if currentHour is equal to 0 then
set currentHour to 12
end if
-- readjust for 12 hour time
if (currentHour > 12) then
set currentHour to (currentHour - 12)
end if
set currentTime to ((currentDate's month) as text) & " " & ((currentDate's day) as text) & ", " & (currentHour as text) & ":" & ((currentMinutes) as text) & " " & amPM as text
if isVoiceOverRunningWithAppleScript() then
tell application "VoiceOver"
output currentTime
end tell
else
say currentTime
delay 2
end if