Random Useful Mac OS X User Tips

Tuesday, 2007-11-27; 02:48:00



Here are some random tips that I've found to be pretty useful:

1. An anonymous hint over at Mac OS X Hints has the hidden defaults command to change the Dock behavior so that you don't have to press the spacebar in order to use springloaded Dock icons. By default, when you hover an item over an icon in the Dock, you have to explicitly press the spacebar to get it to spring open. You can change this to match the Finder's behavior where hovering over the icon will cause it to launch after a small delay. Simply issue the Terminal command defaults write com.apple.dock enable-spring-load-actions-on-all-items -bool YES. You'll need to restart your Dock for this pref to take effect.


2. If you have Leopard, here are two useful Quick Look generators: one generates Quick Look previews for folders, showing all the contents in a list. The other generates Quick Look previews for zip files without having to decompress them: previews made by this plugin show the contents of the zip file. (via digg, twice)


3. Some text editors have a "Paste without Style" menu item, so that you can paste in plain text even if there's additional formatting information for that text in the clipboard. However, most don't. The method I've been using for a long time to strip the formatting information is to open up TextEdit (which automatically creates a new doc in plain text format, if set up right), paste the text in, re-copy it, and then paste it to your destination.

This small AppleScript accomplishes the same thing:

set the clipboard to «class ktxt» of ((the clipboard as text) as record)
beep


I've added a beep to the script so that you know when it's done. Using this script, all you have to do is copy your desired text to the clipboard, invoke this AppleScript, and then paste. Now if you could only reliably assign keyboard shortcuts to items in the script menu...

This AppleScript was first suggested in the comments of this hint.


4. Here's another useful AppleScript if you like making links that automatically create new AppleScripts. I used to have my own script that did this, but it seemed to fail randomly. This one has been completely reliable so far:

-- this script encodes the script text currently on the clipboard
try
        display dialog "Choose the link action and enter the link text:" buttons {"Append", "Insert", "New"} default answer "link text" default button 3
        copy the result as list to {link_text, script_action}
        set this_text to (get the clipboard) as string
        set this_text to my encode_text(this_text)
        if the script_action is "Append" then
                set the URL_opening to (ASCII character 60) & "a href=\"applescript://com.apple.scripteditor?action=append&script="
        else if the script_action is "Insert" then
                set the URL_opening to (ASCII character 60) & "a href=\"applescript://com.apple.scripteditor?action=insert&script="
        else if the script_action is "New" then
                set the URL_opening to (ASCII character 60) & "a href=\"applescript://com.apple.scripteditor?action=new&script="
        end if
        set the clipboard to (URL_opening & this_text & "\"" & (ASCII character 62) & link_text & (ASCII character 60) & "/a" & (ASCII character 62))
        display dialog "The encoded script has been placed on the clipboard." buttons {"OK"} default button 1 with icon 1 giving up after 2
on error error_message
        display dialog error_message buttons {"Cancel"} default button 1
end try

-- this sub-routine is used to encode text 
on encode_text(this_text)
        set the acceptable_characters to "abcdefghijklmnopqrstuvwxyz0123456789_"
        set the encoded_text to ""
        set the character_list to {}
        repeat with this_char in this_text
                set this_char to the contents of this_char
                if this_char is in the acceptable_characters then
                        set the end of the character_list to this_char
                else
                        set the end of the character_list to encode_char(this_char)
                end if
        end repeat
        return (the character_list) as string
end encode_text

-- this sub-routine is used to encode a character 
on encode_char(this_char)
        set the ASCII_num to (the ASCII number this_char)
        set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
        set x to item ((ASCII_num div 16) + 1) of the hex_list
        set y to item ((ASCII_num mod 16) + 1) of the hex_list
        return ("%" & x & y) as string
end encode_char


Copy the script you want to the clipboard, then invoke this script. It'll automatically create the HTML code for the link that creates the AppleScript. You can choose to have it append the script in the URL to the existing active script document, to insert it at the insertion point of the active script document, or create a completely new script document with the script.

I believe this is an Apple-supplied AppleScript, but I don't remember exactly where I got it from anymore.


5. I noted this before, but if you are having troubles with the Finder's "Open With" contextual menu, or with documents associating themselves with the wrong app, or with icon problems, resetting the LaunchServices database can help. (This is sort of the equivalent of rebuilding the desktop from OS 9.)

Here's the command you use in Leopard:


/System/Library/Frameworks/CoreServices.framework/\
Frameworks/LaunchServices.framework/Support/lsregister \
-kill -r -domain local -domain system -domain user

And for Tiger and earlier:


/System/Library/Frameworks/ApplicationServices.framework/\
Frameworks/LaunchServices.framework/Support/lsregister \
-kill -r -domain local -domain system -domain user

Don't worry, you can copy those multi-line commands and paste them directly into the Terminal. No reformatting required.


6. This is a huge one for you UI AppleScripters out there. Automator in Leopard has a new "Watch Me Do" feature that allows you to record UI actions as you perform them, which you can save for later and run again to achieve the same effect. This is completely different from Script Editor's "record" feature which actually watches for AppleEvents; Script Editor's recording requires a scripting dictionary to have been set up for the various applications, and the vast majority of applications don't have such a dictionary.

UI scripting basically means that you script the clicking of buttons or the selecting of objects or the manipulation of windows directly, instead of using an application's scripting dictionary to perform an action. UI scripting is much more involved and verbose, and it actually replicates the user interface actions; using a scripting dictionary is completely independent of the user interface and is much cleaner — unfortunately, as mentioned earlier, most applications don't include a scripting dictionary.

In Tiger and earlier, most UI AppleScripters typically used PreFab's UI Browser to manually figure out the UI scripting syntax for doing various things. And this was, obviously, a pain; Automator's "Watch Me Do" in Leopard allows you to simply perform the user interface actions you want to do, and Automator will record them.

I lamented this in one of my posts about Leopard, but I noted that there didn't seem to be a way to directly access the AppleScript code from the Watch Me Do feature — Automator only creates run-only plugins or scripts. This is annoying if you want to modify the UI script later to do something slightly different. In this situation, you'd have to redo the whole set of actions all over again.

But a commenter named Alex noted a very obscure "feature" in Automator that actually allows you to get at the AppleScript code created from Watch Me Do. Here's how it works:

  • Open Automator and select "Custom" from the Starting Points sheet
  • Click the "Record" button in the Automator window for your new workflow. This will popup a floating panel showing that Automator is now recording your actions, and it'll automatically switch you to the Finder.
  • Perform the desired actions.
  • Click the stop button in the floating Watch Me Do panel. This will return you to Automator and create a new "Observe my actions" step in your Automator workflow.
  • In this "Observe my actions" step, select all of the events in the list. These represent all the separate interface actions that the AppleScript performs.
  • Now, here's the obscure trick: drag the selected events into the empty space in the window for additional steps in your workflow. After a bit, Automator will add an "Execute AppleScript" step in your workflow and guess what? The AppleScript code in this new step represents the UI actions that were recorded using Watch Me Do. You can now copy this code into a regular AppleScript and modify to your heart's content!

I consider this so useful that I want to make sure I describe the trick correctly. If you didn't understand that last step, watch this movie.

Basically, PreFab's UI Browser is no longer needed for creating UI scripts, and you no longer have to manually figure out how to UI script! Yes! This, combined with the fact that UI scripts in Leopard no longer require "delay" lines between each action, makes UI scripting much less painful.


Technological Supernova   Tips   Older   Newer   Post a Comment