Work around an AppleScript bug in iTunes 9.0.2

As noted in this blog post, there is a confirmed bug in iTunes 9.0.2 that prevents Application Applescript bundles from displaying in the iTunes Scripts menu. I won't go into the details of the bug (as Doug Adams has already done a fine job in his linked post -- thanks, Doug!) -- but this is particularly troublesome in Snow Leopard, as you can't create "plain" application scripts. If you make an alias to your iTunes Scripts folder (~/Library/iTunes/Scripts) and drop it in your user's Scripts folder (~/Library/Scripts), then as long as you have the system-wide Scripts Menu enabled, you'll be able to access all of your iTunes scripts (including ones that don't show up in iTunes 9.0.2), and any you might add, from the system Scripts Menu. When Apple fixes the bug, simply delete the alias from your ~/Library/Scripts folder (or forget about it, as there's no harm or du...

published on Thursday, the 10. December 2009, macosxhints

Uninstall an AppleScript application from within Itself

If you make tiny apps that you distribute to friends and family who aren't particularly Mac-literate, you might find this snippet of code helpful: set theUNIXPath to path to me as alias set thePosixPath to (POSIX path of theUNIXPath) as string set thePosixPath to (items 1 thru ((length of thePosixPath) - 1) of thePosixPath) do shell script "sleep 1 &> /dev/null & mv " & thePosixPath & " ~/.trash/YOURAPPNAMEGOESHERE.a​pp" return Basically, any app with that code inserted (and properly activated based on response to a user's action) moves itself to the trash. How it works: it calls sleep 1 with a bash redirection command &> /dev/null &, which initiates the script, but returns to the app immediately. The effect is that your AppleScript app will e...

published on Wednesday, the 9. December 2009, macosxhints

Create a regular playlist from an iTunes Genius Mix

This is just the absolute basics, but this AppleScript will repeatedly hit the Next Track button for you, and then add the current track to a pre-made playlist. You'll have to have the genius mix already playing, and a playlist named Genius_Mix already created before running the script. tell application "iTunes" set pl to playlist "Genius_Mix" of source "Library" repeat with i from 1 to 30 set t to current track if class of t is file track then set loc to (location of t) as alias add loc to pl end if next track end repeat end tell I'll probably get around to making it nicer, but if you're itching for a playlist now, it should do the job.

published on Thursday, the 8. October 2009, macosxhints

Flatten a folder's structure via AppleScript

The following AppleScript traverses the currently-selected folder in the Finder, moves the contents of all subfolders of that folder up to the top level, then moves the (now empty) subfolders to the trash. tell application "Finder" activate set theTopFolder to (selection as alias) --display dialog (theTopFolder as text) repeat with EachSubDir in (get every folder of folder theTopFolder) try --display dialog (EachSubDir as text) repeat with Eachfile in (get every file of folder (EachSubDir as alias)) try move Eachfile to theTopFolder end try end repeat delete folder (EachSubDir as alias) end try end repeatend tell[robg adds: Copy and pas...

published on Tuesday, the 12. May 2009, macosxhints

Open current Finder folder in Path Finder

I use Cocoatech's Path Finder as my primary file browser, reverting to the Finder only on rare occasions. Sometimes, though, I'll open a window (and navigate into some folder) in the Finder by force of (20 year) habit. I wanted a simple way to open that same folder in Path Finder, so I wouldn't have to navigate to it twice.After some Googling, I stumbled on the solution in this forum thread on the Cocoatech forums. User maaku posted this very handy AppleScript:on run tell application "Finder" activate set this_folder to (the target of the front window) as alias close the front window end tell tell application "Path Finder" activate open this_folder end tellend runCopy and paste that code into Script Editor, then save it as an application (File Format » Application in the Save dialog) in a convenient spo...

published on Friday, the 24. April 2009, macosxhints

Move selected items into ready-to-rename folder

This is a simple AppleScript for the Finder which moves the currently selected items into a newly-created folder, and puts the new folder into rename mode. I use this to quickly sort my files into properly-named folders.(*Move items into new folderAuthor: Barry ElsVersion: 1.1*)tell application "Finder" try set theSelection to selection set currentPath to ((the first item of the theSelection) as alias) set parentPath to currentPath if (currentPath as string) ends with ":" then -- it is a folder set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"} set the parentPath to (text items 1 thru -3 of (currentPath as string)) as string set AppleScript'...

published on Thursday, the 2. April 2009, macosxhints

Add folders to the iTunes library via the command line

If, like me, you like to make yourself feel all l33t by doing a lot of system and file management on the command line, you'll have been frustrated at least once by failing to be able to add freshly-downloaded MP3s to the iTunes library (once you've salted them away in your carefully-ordered music stash, naturally) via Terminal.Well, be frustrated no more! Here is a handy-dandy AppleScript I wrote to do just that right in the shell. It takes the path to your newly-minted MP3 folder as an argument, and here's the code:on run named_folder set folder_alias to POSIX file named_folder tell application "iTunes" add folder_alias to library playlist 1 end tellend runOn Leopard, save this to a text file with this as the first line:#!/usr/bin/osascriptThen chmod a+x the script to make it executable, and you can invoke this as you would any shell script:~/scripts/addToITunesLi​brary ~/Music/mp3/_purchased/Malcolm M...

published on Thursday, the 4. September 2008, macosxhints

Put the quoted form of POSIX paths into Script Editor

As a beginner learning the basics of do shell script in AppleScript, I'm finding that most of my file and folder names have spaces in them. As such, I have two choices when writing paths: either escape all the spaces as I type them, or use the AppleScript quoted form of POSIX path construct.Both methods still require significant typing and exact spelling, and I'm from the generation that didn't learn to type. To ease my discomfort, I wrote this script for use in Script Menu:tell application "Finder" set Itms to selection set als to Itms's item 1 as alias set aPath to quoted form of POSIX path of alsend telltell application "Script Editor" activate tell front document set contents of selection to aPath end tellend tellThe script takes the currently-selected file or folder in Finder, and puts its quoted form of POSIX path into Script Editor's front document at the insertion point (or replaces the selection if text is selected)...

published on Friday, the 29. August 2008, macosxhints

10.5: AppleScript 'path to me' function changed in 10.5

When using AppleScript, you sometimes want the running script (or AppleScript Application) to get the path to itself or a file in its resources folder. This is done by using the path to me command: set beep_sound to alias ((path to me as text) & ¬ "Contents:Resources:Sounds:bee​p.mov") In 10.4, a problem I always found when writing one of these script/apps was that if you tried to test run them in Script Editor, the path to me function would return the path to Script Editor. There where ways around this, but it meant editing the script/app just to test. In 10.5, however, as long as you save the script/app first, then the path to me function will return the correct path to the script/app when tested in Script Editor. As an example, this code... "display dialog ("The path to me is : " & return & (pa...

published on Thursday, the 28. February 2008, macosxhints

10.5: Call Automator workflows from AppleScript

Calling Automator workflows from AppleScript has been covered here before for 10.4. However, that tip doesn't work in Leopard.To execute an Automator workflow from an AppleScript in Leopard, first save the workflow as an application, then write a script like this:set lf to path to library folder from user domaintell application "Finder" to open (alias (lf & "Scripts:foo.app:" as text))[robg adds: I haven't tested this one.]

published on Thursday, the 6. December 2007, macosxhints