Archive for the 'Applescript' Category

[Snippet] Check if application is running

Saturday, June 2nd, 2007

Parameters

  • appname - [String] The name of an application. This must be the executable name although AppleScript’s string matching is case-insensitive.

Return value

This returns a boolean value where true indicates that the application is running and false indicates that it wasn’t.

Snippet

on appIsRunning(appName)
    tell application "System Events" to return (name of every process) contains appName
end

appscript: Python in Applescript territory

Sunday, May 6th, 2007

Appscript is a technology that bridges Applescript’s best feature with Python, Ruby, and Objective-C. It brings Apple Events to these languages and allows them to interact with applications similar to Applescript. There’s been a bit of press on MacDevCenter about using Appscript in Ruby. That’s all well and good but I don’t particularly care for Ruby. I’ve wanted to see some tutorial about using appscript with Python. I’ve also wanted to see how to interact with everyone’s favorite scriptable application, iTunes. I haven’t seen either so I spent part of today working something up.

Appscript is interesting and somewhat exciting. Its biggest hassle is the marriage between two different language styles. Applescript tries to be verbose and English-like. Python, Ruby, and Objective-C all have to do some peculiar things to approximate this readability. They don’t mesh particularly well, although appscript does a reasonably admirable job. If you are already an applescripter, you have a hurdle to get around. The different syntax makes you think in a different way than you normally would. Of course, if you are used to “normal” languages, you also have to reorient yourself to think a bit like an applescripter.

I wrote a quick little script that renames tracks for a particular artist to that it follows normal title format considering common prepositions and whatnot. The script and some comments are in the extended entry.

(more…)

[Snippet] Upload using Transmit

Friday, April 13th, 2007

Very simple snippet to send a list of files to a server using Transmit.

Parameters:

  • favoriteName - [String] Name of a Transmit favorite. This snippet only does favorites.
  • filesToUpload - [List] A list of all the files that you want to go to a particular place on the server.
  • location - [String] The path that you want the files to be at. Since each upload is a new session, this tends to be relative to the root directory.

Snippet:

on uploadViaTransmit(favoriteName, filesToUpload, location)
    tell application "Transmit"
        set SuppressAppleScriptAlerts to true
        set doc to make new document at end
        tell current session of doc
            with timeout of 60 * 60 seconds
                if (connect to favorite with name favoriteName) then
                    set their stuff to location
                    repeat with f in filesToUpload
                        upload item f with resume mode replace
                    end repeat
                    disconnect
                end if
            end timeout
        end tell
        close doc
    end tell
end uploadViaTransmit

`display dialog` crashes Mail.app *or* How to log messages to the Console from Applescript

Thursday, April 12th, 2007

If you are writing a mail rule for Mail.app that uses Applescript, don’t use display dialog. In my experience, it’s a fairly reliable crash. Also, Image Events tends to bring Mail.app down. Both of these types of crashes occur in some CG- code so I suspect that they’re related. Anyway, that creates a bit of a debugging problem. If you want simple debugging, you can log messages to the Console using the following snippet:

on clog(msg)
    set uid to do shell script "id -u"
    do shell script "echo " & quoted form of msg & " >> /Library/Logs/Console/" & uid & "/console.log"
end clog

Now, you can open Console.app and watch messages stack up whenever you send my clog("Debug!").

Note that Applescript already has a log function for logging to the Script Editor console but this logging does not show up in Console.app.

Must share snippets

Tuesday, April 10th, 2007

I wrote a little plugin for Apple’s Script Editor a while back that adds a Code Snippets option to the Window menu. Not to overly pimp my own code but I find it exceptionally useful and recommend that every scripter give it a shot. There’s a dozen or so pieces of code that goes into nearly every semi-complex script that I do. I’m releasing a new script at the end of the week (it needs some testing) and have refined several of my snippets. I’m out of ideas for my blog posts on Applescript (other than a few minor items and warnings that I’ve been meaning to write about) so I thought I’d share my snippets. If anyone else uses the Code Snippets plugin, I’d love to hear your thoughts on it as well as read some of your snippets.