AppleScript Snippets
A collection of useful AppleScripts including text parsing, XML processing, and file management utilities.
Below is a collection of useful AppleScripts. It includes some simple ways to search text, parse XML, or load a text file to a sting.
Sort Clipboard Contents Applescript
(* This script will convert anything in the finder to text, and then sort that text. Procedure is to copy an item. Run this script from the script menu, then paste.
*)
set the clipboard to list_to_string(ASCII_Sort(string_to_list(«class ktxt» of ((the clipboard as text) as record), return)), return)
on ASCII_Sort(my_list)
--from apple
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end ASCII_Sort
Export Apple Mail to Gmail Applescript
(*
This script will automate the forwarding of your Mail.app email messages to your
gmail account. Keep in mind that it will take 2 seconds per email so make sure you
allow yourself enough time. For more information on the gml.py necessary for this
script, go to http://www.marklyon.org/gmail/.
*)
-- Your Gmail email address
property myGMailAddress : "youremail@gmail.com"
-- Path to your Mailboxes
property mymailboxpath : "/Users/yourusername/Library/Mail/Mailboxes/"
--Path to the downloaded gml.py file
property gmlFilePath : "/Users/yourusername/Desktop/gml.py"
--Exclude mailboxes that contain any of these words add any words you like
property myExcludedMailboxesList : {"cache", "junk", "deleted", "drafts", "sent", "outbox", "info.plist", ".DS_Store"}
-- Don't change this unless you know what you're doing
property alternateSMTPServer : "gsmtp171.google.com"
Applescript String Parsing
(**** Example ****)
-- this example will find the word "work" in the string "Bob went to work." and replace it with "the beach".
set myResult to snr("Bob went to work.", "work", "the beach")
display dialog myResult
(**** fast search and replace methods ****)
on snr(the_string, search_string, replace_string)
return my list_to_string((my string_to_list(the_string, search_string)), replace_string)
end snr
on list_to_string(the_list, the_delim)
my atid(the_delim)
set the_string to (every text item of the_list) as string
my atid("")
return the_string
end list_to_string
on string_to_list(the_string, the_delim)
my atid(the_delim)
set the_list to (every text item of the_string) as list
my atid("")
return the_list
end string_to_list
on atid(the_delim)
set Applescript's text item delimiters to the_delim
end atid
Better Mount Volumes script
set requiredDrives to {{theName:"drive1name", theMountscript:"afp://username:password@ip/drive1name"}, {theName:"drive2name", theMountscript:"afp://username:password@ip/drive2name"}} as list
set theDisks to list disks
repeat with i from 1 to (length of requiredDrives)
if theDisks does not contain theName of item i of requiredDrives then
mount volume theMountscript of item i of requiredDrives
end if
end repeat
Fast Is File Busy Check (Mac OS X)
on isFileBusy(thePath)
--Tests to see if a file is in use
try
set myscript to "if ( lsof -Fp " & thePath & " | grep -q p[0-9]* ) then echo 'file is busy'; else echo 'not busy';fi"
set myResult to do shell script myscript
if myResult is "file is busy" then
return true
else
return false
end if
on error err
Display Dialog ("Error: isFileBusy " & err) giving up after 5
end try
end isFileBusy
Load Text File to String
(* The purpose of this handler is to load a text file from a posix path to a string*)
on loadTextFile(thepath)
try
set thestring to ""
set thescript to ("cat " & "'" & thepath & "'")
set thestring to do shell script thescript
return thestring
on error err
log ("Cat text file error: " & err)
end try
end loadTextFile
Remove dot-3 Extension from File Name
(*
The purpose of the is handler is to accept a file name as a parameter and return the filename without the dot 3 name extension (eg on removeExtension("filename.txt") returns the string "filename")
*)
on removeExtension(theString)
set N to count of characters of theString
if (N > 3) and character (N - 3) of theString is "." then
set theString to characters 1 thru (N - 4) of theString
set theString to theString as text
else
--there isn't an extension
end if
return theString
end removeExtension
Additional AppleScript utilities and examples are available in the full source file.