質問

I made a script for Preview and it is working perfect. It opens the image and then waits until the document in Preview is closed.

After that I tried the same with Photoshop, but it is not working there:

tell application "Finder"
    try
        set appID to application file id "com.adobe.Photoshop"
        --set appID to application file id "com.apple.Preview"
    on error errMsg
        set appID to 0
    end try
end tell
tell application "Finder" to set appName to name of appID
tell application appName
    run
    activate
    set fileHandle to open POSIX file pngFile as alias
    repeat
        -- exit repeat
        try
            get name of fileHandle
        on error
            exit repeat
        end try
        delay 1 -- delay in seconds
    end repeat
end tell
display dialog "Document is closed now"

Any ideas what is going wrong or even better how to check in Photoshop if a certain file is still open?

役に立ちましたか?

解決

If you want to open a file and delay until the file is actually open in Photoshop then you've got some problems with your code. First, if it is to work how you're thinking then your "exit repeat" line is in the wrong place. It shouldn't be in the "on error" portion of the try block. The purpose of your repeat loop and try block is to wait until you can get the name of the file without error... meaning the file is open... then exit repeat. So your repeat loop should look like this...

repeat
    try
        get name of fileHandle
        exit repeat
    end try
    delay 1 -- delay in seconds
end repeat

However you have other mistakes in your code so even with that fix it still wouldn't work. One big mistake is fileHandle. Photoshop's open command does not return a reference to the file so when you "get name of fileHandle" that will error no matter what because there is no fileHandle.

Here's how I would write your code. You don't need any Finder stuff and you certainly shouldn't put the Photoshop code inside the Finder code. Anyway, try this. I hope it helps.

set filePath to (path to desktop as text) & "test.jpg"

set fileOpen to false
tell application id "com.adobe.Photoshop"
    activate
    open file filePath

    set inTime to current date
    repeat
        try
            set namesList to name of documents
            if "test.jpg" is in namesList then
                set fileOpen to true
                exit repeat
            end if
        end try
        if (current date) - inTime is greater than 10 then exit repeat
        delay 1
    end repeat
end tell
return fileOpen
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top