 |
 |
|
Overview |
 |
|
Game of Life |
 |
|
TV episodes |
 |
 |
Overview |
 |
 |
Star Trek TNG |
 |
 |
The Trap Door |
 |
 |
Willo the Wisp |
 |
 |
|
OS X Terminal |
 |
|
AppleScripts |
 |
|
Foxy :-) |
 |
|
Pi to 100 |
 |
|
Links |
|
 |
 |
 |
 |
Useful AppleScripts
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.
 |
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:
 |
 |
 |
 |
tell application "Finder"
update the items of window 1
end tell
|
 |
 |
 |
 |
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.
 |
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:
 |
 |
 |
 |
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
|
 |
 |
 |
 |
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:
 |
 |
 |
 |
"/Users/michaelhogg/Desktop/Dad's photos/"
"'/Users/michaelhogg/Desktop/Dad'\\''s photos/'"
|
 |
 |
 |
 |
 |
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:
Text copied from a webpage will produce a list of data formats like this:
 |
 |
 |
 |
{{Unicode text, 298}, {styled Clipboard text, 182}, {string, 150}, {uniform styles, 1104}, {«class ut16», 302}, {«class utf8», 149},
{«class RTF », 1598}}
|
 |
 |
 |
 |
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:
 |
 |
 |
 |
{{picture, 124720}, {TIFF picture, 122958}}
|
 |
 |
 |
 |
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:
 |
 |
 |
 |
the clipboard as «class utf8»
|
 |
 |
 |
 |
Replace «class utf8» with the data format that you wish to view. A couple of examples are shown below:
 |
 |
 |
 |
the clipboard as string
"Hello world!"
the clipboard as «class RTF »
«data RTF 7B5C727466315C6D61635C616E73696370673130»
|
 |
 |
 |
 |
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:
 |
 |
 |
 |
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
|
 |
 |
 |
 |
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).
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 |
|
 |
 |
 |
 |
|