5 Replies Latest reply: Jun 6, 2008 6:41 PM by dev_sleidy
tjrazor Level 1 Level 1 (0 points)
I'm having trouble with what I thought would be a simple script. All I want to do is create a simple script that moves Pages templates into the appropriate template folder. I keep getting the following error:

"Applescript Error
Finder got an error: Handler can't handle objects of this class"

Here's the script that has the error:

set theFile to choose file
set theFolder to path to application support from user domain
set thePath to theFolder & "iWork:Pages:Templates:My Templates:"
display dialog thePath as string -- displays correct path the the folder I want
tell application "Finder"
move theFile to thePath
end tell

I appreciate any help I can get.

thanks,

G4 Tower, Mac OS X (10.4.11)
  • 1. Re: File paths and moving files
    Camelot Level 8 Level 8 (45,790 points)
    You've run into one of those subtle coercion issues in AppleScript.

    The problem lies with the line:

    set thePath to theFolder & "iWork:Pages:Templates:My Templates:"


    In this case, theFolder is an alias (the result of the path to command) and "iWork:Pages...." is a text object. When you concatenate two different classes (e.g. alias and string) the result is a list:

    -->{alias "HD:users:username:Library:Application Support:", "iWork:Pages:Templates:My Templates:"}


    Therefore, the move command fails because you're telling the Finder to move a file into a list, which can't be done.

    The solution is quite simple - be more expilict about what thePath is. In this case, simple appending "as text" to the line fixes the problem:

    set thePath to theFolder & "iWork:Pages:Templates:My Templates:" as text


    Now thePath is a single text object which the Finder will coerce to a folder path when used in the move command. If you want to be even more explicit, you could:

    move theFile to folder thePath
  • 2. Re: File paths and moving files
    tjrazor Level 1 Level 1 (0 points)
    Thanks so much for your help. I thought I had tried that, but now I see I had "as string" rather than "as text" I rarely work with Applescript and I would have thought those were the same. Anyway, the script now works and I'd like to make one adjustment. I'd like to create a new folder and put the file in that folder. Here's the script, but I'm getting a different error. Hopefully it's just a silly mistake.

    set theFile to choose file
    set theFolder to path to application support from user domain
    set thePath to theFolder & "iWork:Pages:Templates:" as text
    display dialog thePath as string
    set baseFolder to make new folder at thePath with properties {name:"GS Templates"} -- added this
    tell application "Finder"
    move theFile to folder baseFolder
    end tell

    Error I'm getting is:
    "AppleScript Error
    Can't make current application into type location reference."

    I appreciate your time,
  • 3. Re: File paths and moving files
    Camelot Level 8 Level 8 (45,790 points)
    This one's quite simple - AppleScript can't create folders - at least not on it's own.

    You need the Finder to make the folder for you. Therefore the 'make new folder' line needs to be inside the 'tell application "Finder"' block.
  • 4. Re: File paths and moving files
    tjrazor Level 1 Level 1 (0 points)
    I tried the following:

    set theFile to choose file
    set theFolder to path to application support from user domain
    set thePath to theFolder & "iWork:Pages:Templates:" as text
    display dialog thePath as string
    --set baseFolder to make new folder at thePath with properties {name:"GS Templates"}
    tell application "Finder"
    make new folder at thePath with properties {name:"GS Templates"}
    end tell
    set finalFolder to thePath & "GS Templates:" as text
    tell application "Finder"
    move theFile to folder finalFolder
    end tell

    Error:
    "AppleScript Error
    Finder got an error: Can't make class folder."

    Once again, I appreciate your help.
  • 5. Re: File paths and moving files
    dev_sleidy Level 4 Level 4 (1,570 points)
    Enter the code below ...

    -- Code starts here --

    property tPath : ((path to application support from user domain) as string) & "iWork:Pages:Templates:My Templates:"

    on run {} -- Allow user to select multiple files.
    my handle_items(choose file with multiple selections allowed)
    end run

    on open (dItems) -- Allow user to drag multiple files onto applet.
    my handle_items(dItems)
    end open

    on handle_items(localItems)
    repeat with i in localItems -- Move selected items.
    try
    tell application "Finder" to move item i to folder tPath
    end try
    end repeat
    end handle_items

    -- Code ends here --

    ... and save as an application (AppleScript applet).

    Now, you can double click on the applet for manual, or drag items onto the applet for automatic, processing.