我为Preview编写了一个脚本,它运行完美。它打开图像,然后等待,直到“预览”中的文档关闭。

之后,我在Photoshop中尝试了相同的操作,但是在这里不起作用:

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"


有什么想法出了什么问题,或者甚至更好的如何在某个文件仍打开的情况下在Photoshop中进行检查?

最佳答案

如果要打开文件并延迟到在Photoshop中实际打开文件,则代码会出现问题。首先,如果要按照您的想法进行工作,那么“退出重复”行就在错误的位置。它不应位于try块的“ on error”部分。重复循环和try块的目的是等待直到获得文件名而没有错误...意味着文件已打开...然后退出重复。因此,您的重复循环应如下所示:

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


但是,您的代码中还有其他错误,因此即使进行了修复,它仍然无法正常工作。一个大错误是fileHandle。 Photoshop的open命令不会返回对该文件的引用,因此,当您“获取fileHandle的名称”时,由于没有fileHandle,无论如何都将出错。

这就是我编写您的代码的方式。您不需要任何Finder东西,当然也不应该将Photoshop代码放入Finder代码中。无论如何,尝试一下。希望对您有所帮助。

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

10-07 19:11
查看更多