AppleScript Problem

Saturday, 2005-12-17; 01:32:00


Stupid AppleScript.

OK this is pissing me off, and I want some help.

I got the following Resize and Mail script off of Complete Digital Photography, which, incidentally, seems to be at least a halfway decent weblog about digital photography on the Mac. I've presented it here as a modified script -- instead of resizing the image and attaching it to a new mail message, it simply resizes the dropped image and then saves it right next to the original image (with an extra "thumb" suffix appended on to the end). I also changed the file naming code so that it's more reliable at choosing a name in the Finder that is unique, since the original seemed to have a problem with that.

--Resize and Mail
--by Ben Long
--www.completedigitalphotography.com

-- Modified to simply be a resize droplet by Simone Manganelli

on open theFiles
        global theName
        global myTempFileName
        
        repeat with aFile in theFiles --loop through all of the selected files
                tell application "Image Events"
                        launch
                        set this_image to open aFile --resize the image
                        scale this_image to size 500
                        set AppleScript's text item delimiters to {"."}
                        set the_count to count of text items of name of aFile
                        tell application "Finder" to set theName to name of aFile as string --create a new name and save path
                        set theNameNoExt to text item 1 of theName as string
                        tell application "Finder" to set myTempFileName to (home as string) & "Desktop:" & theNameNoExt & "thumb"
                        tell application "Finder"
                                if (file myTempFileName exists) then --make sure that the name we want isn't already being used. If it is, make a new one
                                        set nameCounter to 1
                                        set done to false
                                        repeat until done
                                                set myTempFileName to (home as string) & "Desktop:" & theNameNoExt & "thumb" & nameCounter
                                                if not (file myTempFileName exists) then
                                                        set done to true
                                                else
                                                        set nameCounter to nameCounter + 1
                                                end if
                                        end repeat
                                end if
                        end tell
                        set myTempFileName to myTempFileName & "." & (name extension of aFile)
                        save this_image in file myTempFileName with icon
                        close this_image
                end tell
        end repeat
end open


This AppleScript is saved as a droplet, so that you can just drag and drop images onto the droplet icon, and the images will automatically be resized. This is basically creating a thumbnail, not resizing the actual image, because the original image is left untouched.

The problem is that a droplet isn't as useful for me as it could be. I was leaving it on my Desktop, but I've been trying to keep my Desktop tidy and so I thought I'd move this handy little script into my Scripts menu. That way, I'll just select the images for which I want to create thumbnails, and then I'll select this script from my Scripts menu. That means the code so that it's not a droplet anymore. Here's what I tried:

set theFiles to (selection of application "Finder")
global theName
global myTempFileName

repeat with aFile in theFiles --loop through all of the selected files
        tell application "Image Events"
                ...
        end tell
end repeat


Note that the "..." just means that the same code is used inside that tell block. Actually, the only things I changed was I removed the on block from the droplet, and I added the first line to grab the selection from the Finder.

Unfortunately, this doesn't work. If I run it from Script Editor, it opens the original image in Preview for some reason, and then throws up a "The variable this_image is not defined" error. WTF? I'm assuming that on open block in a droplet takes the same arguments as the Finder selection line returns -- it's a list of documents in the following format:

{document file "accretion.jpg" of folder "Desktop" of folder "user1" of folder "Users" of startup disk of application "Finder"}

I've inserted a display dialog theFiles as string line in both the droplet and in the regular script, and they both return a path to the file as an AppleScript path. That doesn't mean that the type of argument needed is the same, but it does at least give some evidence that the type is probably correct. Moreover, why is AppleScript complaining that "this_image" is undefined? I mean, it's defined in the immediately previous line! I was thinking that there was some error in opening the file, but then why would it work in the droplet on the same file in the same location and not in the regular script? GRR!!!

(By the way, no, I do not want to use Automator. While I probably could and it might work just as well, this little niggling problem will bug me to no end until I fix it.)

Unfortunately, in my experience, I've found that AppleScript in Mac OS X is much more unreliable compared to AppleScript on the Classic Mac OS. Script Editor throws up weird errors like this that I can't seem to figure out, and it also borks in totally lame situations. For example, sometimes I like to "cheat" and create a complicated command such as set ooga to text item 5 of (first item of (get selection of application "Finder") as string) and AppleScript borks on it. However if you split it up into lines like so...

set ooga to (get selection of application "Finder")
set booga to first item of ooga as string
set blah to text item 5 of booga

... then the AppleScript works. I'm not talking specifically about these lines of code, but I'm demonstrating a similar situation to those when AppleScript sometimes borks. There should be no difference whatsoever between the single line and the split lines, but sometimes it seems the split lines are necessary.

Has anybody else been having similar difficulties with AppleScript on Mac OS X?


Technological Supernova   Unfiled   Older   Newer   Post a Comment