applescriptusingfindertell

AppleScript Finder "open using" issue


I'm having some trouble scripting (what I thought would be) a very easy section of something I'm working on. Basically, I want to tell Finder to open a file with a specific application. Simple, right? From what I've read I should be able to use:

tell application "Finder"
    open "the_file" using "the_application"
end tell

The trouble is Finder seems to be having one heck of a time finding the application. When I use the following code:

set webArcExtract to POSIX file (do shell script "mdfind 'WebArchive Folderizer.app' -onlyin '/Applications/'") as string #Find the Web Archive Extraction Program

tell application "Finder" #Temporary path to save the web archive
    set tempPath to ((home as string) & "temp:")
end tell

tell application "Fake" #Magic that saves a webpage as a webarchive
    load URL "www.google.com"
    delay 3
    capture web page as Web Archive saving in tempPath & "arc.webarchive"
end tell

tell application "Finder" #Open the arc.webarchive file saved in the tempPath with the WebArchive Folderizer application
    open tempPath & "arc.webarchive" using webArcExtract
end tell

The values of the variables are as follows:

tempPath: "OSX_Data:Users:user:" webArcExtract: "OSX:Applications:Utilities:WebArchive Folderizer.app"

The error I get when trying to run the code occurs on the open tempPath & "arc.webarchive" using webArcExtract line. A message pops up from Finder stating "The Application cannot be found."

Im really befuddled by this one. I KNOW the path is right and I KNOW the application can open files this way. I can use Finder to go to the arc.webarchive file I'm trying to open, right-click on the file, and select "open with > WebArchive Folderizer" and it works perfectly.

Any suggestions?


Solution

  • Here's a couple suggestions. 1) an easier method to get the path to an application is to just use applescript's "path to" command so something like this should work...

    set theFile to (path to desktop as text) & "a.txt"
    set appPath to path to application "TextWrangler"
    tell application "Finder" to open file theFile using appPath
    

    2) You do not need the Finder for the tempPath, again just use "path to"...

    set tempPath to (path to home folder as text) & "temp:"
    

    3) finally you need a file specifier here, so add the keyword "file" before the file path as I did in #1...

    tell application "Finder"
        open file (tempPath & "arc.webarchive") using webArcExtract
    end tell
    

    I hope that helps.