Search code examples
arrayslistapplescriptwaitphotoshop

Applescript / Photoshop - Some sort of "wait if busy" command?


I have some code that constantly checks for any open documents and adds a comment in finder when it is closed.

I'm having an issue though that if anything like the "Do you wish to save?" or "Loading..." comes up, it's some weird limbo between 0 and 1 and can't seem to count properly. It then crashes with the generic "General Photoshop Error" message.

Is there some sort of "Pause/Wait if busy" command, or some other way around this?

repeat

tell application "Adobe Photoshop CS6"

    set D1 to count documents

    local mainDocList
    set mainDocList to {}

    if ((count mainDocList) ≠ D1) then
        log "Main Ran"
        if (D1 > 0) then
            set c to 1
            repeat while (c ≤ D1)
                set mainDocList to mainDocList & (file path of document c)
                set c to c + 1
            end repeat
        end if
    end if

    delay 3

    set D1 to count documents

    local currentDocList
    set currentDocList to {}

    if ((count currentDocList) ≠ D1) then
        log "Current Ran"
        if (D1 > 0) then
            set c to 1
            repeat while (c ≤ D1)
                set currentDocList to currentDocList & (file path of document c)
                set c to c + 1
            end repeat
        end if
    end if


end tell

local closedList, a
set closedList to {}
if ((count mainDocList) ≠ (count currentDocList)) then
    repeat with a in mainDocList
        set a to contents of a
        if {a} is not in currentDocList then set end of closedList to a
    end repeat
end if
log "∆: " & (count closedList)
log "- - - - -"


if ((count closedList) > 0 and ((count mainDocList) > (count currentDocList))) then
    tell application "Finder"
        set c to 1
        repeat while (c ≤ (count closedList))
            tell application "System Events"
                set modDate to modification date of (item c of closedList)
            end tell

            set theDate to current date

            set Y to text -2 thru -1 of ("00" & (year of theDate))
            set M to text -2 thru -1 of ("00" & ((month of theDate) as integer))
            set D to text -2 thru -1 of ("00" & (day of theDate))
            set spartanDate to Y & M & D

            set theTime to (time string of theDate)
            set theTime to text -11 thru -7 of ("00" & (time string of theDate))

            set currentComments to (get comment of (item c of closedList))
            set comment of (item c of closedList) to currentComments & (" | USER-" & spartanDate & "-" & theTime)
            log "******************COMMENTED"
            set c to c + 1
        end repeat
    end tell
end if


end repeat

Solution

  • Well, it would help if you indicated WHERE in the code it seems to hang. In my opinion, any Applescript code that is dealing with files should be liberally sprinkled with try...on error...end blocks.

    try
        ...
     on error errMsg
        display dialog "ERROR: " & errMsg
    end try
    

    Once you've identified where the problem is triggered, and see the error message, you and we would be better able to fix the problem.

    Also, if you suspect that a modal dialog popping up is part of the problem, encircle that code with a timeout block:

    with timeout of 60000 seconds
       ... could that could otherwise timeout
    end
    

    Every script statement is subject to a timeout, and bad things usually happen if you do not either catch the timeout in a TRY block or think ahead and guard against it with a TIMEOUT block.