 |
 |
|
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 |
Paco 23 Sep 2010 France |
Thanks, now i can save some parts of webpages contents as webarchives |
Leo 24 Feb 2011 Germany |
Just like to state here that the 'clipboard info' command crashes on MacOS 10.5 when the clipboard is TRULY emty.
To truly emty the clipbord open an Excel table with quite a lot of data included. Copy the data to the clipboard, then close the table. You will be asked now if you like to keep the data in the clipboard. Click NO. If now you run the clipboard info command your applescript / script editor will crash, even if you put the command in a 'try block'.
If anyone has more details about this subject, please let me know.
Thanks in advantage |
Michael Hogg 26 Feb 2011 |
Thanks for your comment Leo! I wasn't aware of this phenomenon.
An easy way to check whether the clipboard is truly empty is to use the Finder menu option: Edit > Show Clipboard. If the clipboard is empty, then the bottom-left corner of the window will show: "Clipboard contents: none".
Here's another way to empty the clipboard. Open a very large image in Adobe Photoshop. Select the entire image, and copy it to the clipboard. Then switch to a different application, and Photoshop will give you an error ("Clipboard export failed because it is too big to export", "Clipboard export failed because the image is too wide to save as a PICT", etc). If you now do Show Clipboard in the Finder, you'll see the clipboard is now empty.
The command "clipboard info" will indeed crash the Script Editor application if the clipboard is empty. However, the command "the clipboard" will not cause a crash, but will generate an error of type -25131, which is "badPasteboardIndexErr" (the specified pasteboard item index does not exist).
Here's a little script to test whether the clipboard is empty:
set blnIsClipboardEmpty to false try set dataClipboard to the clipboard on error strErrorMessage number intErrorNumber if (intErrorNumber is -25131) then set blnIsClipboardEmpty to true end if end try
After running this script, blnIsClipboardEmpty will be set to "true" if the clipboard is empty. |
Johnnie Wilcox 24 Jul 2011 United States |
I have an AppleScript that writes the clipboard data to a file. The script runs long enough that the clipboard might change (due to user action). This means that the fourth line of the following code works (note variables "myFile" "filePath" and "fileType" are all defined earlier in the script)
set myFile to (open for access filePath with write permission) set eof myFile to 0 write (the clipboard as fileType) to myFile close access myFile
But this code does not
set clipboardContents to the clipboard set myFile to (open for access filePath with write permission) set eof myFile to 0 write clipboardContents as fileType to myFile close access myFile
Is it possible to copy the full clipboard to an Applescript variable (object?) that contains everything the clipboard contains such that an applescript can successfully run the second code chunk above? |
|
 |
 |
 |
 |
|