FractalNet
Home Software Music Other
Overview   Overview
Conway’s Game of Life: An Illustrated Guide   Game of Life
TV episode information   TV episodes
TV episode information - Overview       Overview
TV episode information - Star Trek: The Next Generation       Star Trek TNG
TV episode information - The Trap Door       The Trap Door
TV episode information - Willo the Wisp       Willo the Wisp
Mac OS X Terminal commands guide   OS X Terminal
Useful AppleScripts   AppleScripts
Foxy :-)   Foxy :-)
How to memorise Pi to 100 places   Pi to 100
Links   Links
Useful AppleScripts

AppleScript Script Editor

AppleScript is a scripting language included in the Mac OS.  The Script Editor application (located in /Applications/AppleScript/) allows you to write and execute AppleScript code to perform some very useful tasks.  On this page, I’m documenting my favourite uses for AppleScript.


AppleScript icon Force-updating the “Date Modified” and “Size” columns in a Finder window 

When you open a document from a Finder window (by double-clicking it, or selecting it and pressing ⌘O), and then modify it and save it, the Finder usually doesn’t immediately update the “Date Modified” and “Size” columns for that document.  This can be rather unhelpful!  However, the code below will force the Finder to update these columns in the frontmost window:

Top left Spacer Top right
Spacer tell application "Finder"
    update the items of window 1
end tell
Spacer
Bottom left Spacer Bottom right

I recommend you save this code as an “application” (using Script Editor) and place it in your Dock.  Then, whenever you want to force-update the “Date Modified” and “Size” columns in the frontmost Finder window, simply click on your application in the Dock.


AppleScript icon Creating a time-lapse sequence of screenshots 

The code below executes the screencapture Terminal command at regular intervals, to create a time-lapse sequence of screenshot images in PNG format:

Top left Spacer Top right
Spacer set f to choose folder with prompt "Screenshots will be saved here"
set f to POSIX path of f
set f to quoted form of f
repeat with c from 1 to 9999
    do shell script "screencapture " & f & "screenshot" & c & ".png"
    delay 30
end repeat
Spacer
Bottom left Spacer Bottom right

Change the 9999 value to specify how many screenshots you want.  Change the delay 30 value to specify the time delay between each screenshot in seconds (eg. delay 3600 would take one screenshot every hour).  Note: the actual delay between screenshots will be a little bit longer than the delay you specify, since the process of actually taking a screenshot and saving it to disk isn’t instantaneous.

See here for my documentation on the screencapture Terminal command.  See here for more information about the do shell script AppleScript command.

In line 1, the choose folder command returns an alias.  In line 2, the POSIX path of the alias is obtained as a string (eg. "/Users/michaelhogg/Desktop/").

In line 3, the POSIX path string is converted into a format called quoted form, which is compatible with the do shell script command.  See here for more info.  Without this conversion, certain characters (' ( ) $ * etc) in the POSIX path string would cause problems in the do shell script command.  Below is an example showing a POSIX path string before and after conversion into quoted form:

Top left Spacer Top right
Spacer "/Users/michaelhogg/Desktop/Dad's photos/"
"'/Users/michaelhogg/Desktop/Dad'\\''s photos/'"
Spacer
Bottom left Spacer Bottom right


AppleScript icon Accessing and saving Clipboard data 

When you copy something to the Clipboard (eg. some text from a webpage), the Clipboard actually stores the data in several different formats (eg. plain text, RTF, etc).

Once you’ve copied some data to the Clipboard, run the following code to obtain a list of the available data formats:

Top left Spacer Top right
Spacer clipboard info Spacer
Bottom left Spacer Bottom right

Text copied from a webpage will produce a list of data formats like this:

Top left Spacer Top right
Spacer {{Unicode text, 298}, {styled Clipboard text, 182}, {string, 150}, {uniform styles, 1104}, {«class ut16», 302}, {«class utf8», 149}, {«class RTF », 1598}} Spacer
Bottom left Spacer Bottom right

The number next to each format indicates the number of bytes of data in that format.  In this example, the plain text string only requires 150 bytes, whereas the RTF format requires 1,598 bytes.

An image copied from a webpage will produce a list of data formats like this:

Top left Spacer Top right
Spacer {{picture, 124720}, {TIFF picture, 122958}} Spacer
Bottom left Spacer Bottom right

Images in the Clipboard are stored in two formats: picture (PICT) and TIFF picture (TIFF).

To view the actual data in the Clipboard, run this code:

Top left Spacer Top right
Spacer the clipboard as «class utf8» Spacer
Bottom left Spacer Bottom right

Replace «class utf8» with the data format that you wish to view.  A couple of examples are shown below:

Top left Spacer Top right
Spacer the clipboard as string
"Hello world!"

the clipboard as «class RTF »
«data RTF 7B5C727466315C6D61635C616E73696370673130»
Spacer
Bottom left Spacer Bottom right

Note that many data formats, such as RTF and images, will be displayed as hex data rather than readable text.

To save Clipboard data to a file:

Top left Spacer Top right
Spacer set d to the clipboard as «class utf8»
set fn to choose file name
set fid to open for access fn with write permission
write d to fid
close access fid
Spacer
Bottom left Spacer Bottom right

Replace «class utf8» with the data format that you wish to save.  This code will prompt the user to choose a location and name for the output file.  The file name should end with the appropriate extension (.txt for a plain text file, .rtf for an RTF file, .pict for a PICT image, .tiff for a TIFF image, etc).


Comments Automatic country lookup for comments powered by MaxMind GeoLite Country

Jon Kleiser
    19 Sep 2009
    Norway
I was actaully looking for an AppleScript function that would format an integer with leading zeros so that the resulting string would always be of a given length, e.g. 001, 002, 003... Do you know a nice and clean way to do it?
Jon Kleiser
    20 Sep 2009
    Norway
For now, this is good enough for me:

on sFormat(n, w)
-- provides leading zeros to get total width w
return text -w thru -1 of ("00000" & n)
end sFormat

I found the hint at macscripter.net.
Jon
    27 Jan 2010
    Canada
This is great! I have been searching for days to save a web browser selection as HTML. I want the html styling but TextEdit can't save html documents with embeded images. Now I should be able to save a rtf document then strip out the images before saving as html.
Thanks


Add a comment 

Name (optional)
Copy the security code above
into the box below:



The security code is a CAPTCHA to prevent automated software from posting large amounts of spam comments (I get about 22 spam comments for every 1 genuine comment).
Comment
No HTML or BBCode please
Contact me  –  Page last updated on 29th January 2009  –  Website hosted by 5quidhost (highly recommended!)
Michael Hogg