Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question:

Question: How can I debug a folder actions applescript

I must be missing something pretty basic because I cannot see how to debug a folder actions applescript. My starting script is based on the add - new items alert.scpt but when I run it I cannot see the log file.

MacBook Pro with Retina display, OS X El Capitan (10.11.6)

Posted on

Reply
Question marked as Solved
Answer:
Answer:

Probably the biggest issue is that folder actions don't run in the Script Editor, so it is difficult to use normal debugging techniques. What I do is separate the code that the action will perform from handers that call it, so that it can be run in different ways - for example:


# application double-clicked or script run from the Editor
on run
  doStuff for (choose file with multiple selections allowed) -- or choose folder, etc
end run

# items dropped onto the application
on open theFiles
  doStuff for theFiles
end open

# folder action - handle items added to a folder
on adding folder items to this_folder after receiving these_items
  doStuff for these_items
end adding folder items to

# do something with a list of file items
to doStuff for someFiles
  repeat with anItem in someFiles
    set anItem to anItem as text -- handle text, alias, POSIX, etc
    tell application "System Events" to set {theClass, theName} to {class, name} of disk item anItem
    if (theClass as text) is in {"folder", "«class cfol»"} then -- a folder (not a file package)

      log "folder:" & tab & theName -- do something with a folder

      tell application "Finder" to set folderItems to (items of folder anItem) as alias list
      doStuff for folderItems

    else -- a file

      log "file:" & tab & theName -- do something with a file

    end if
  end repeat
end doStuff

Posted on

Page content loaded

Nov 11, 2017 12:52 AM in response to jbh2 In response to jbh2

Hi jbh2


It's not straightforward - one way is to insert error handlers which return dialogs throughout your script


User uploaded file


or you could include dialogs at strategic points to check progress, then remove them when you're happy that the script is working as intended.


User uploaded file


Or you could post your script here - someone might be able to help.


Cheers,


H

Nov 11, 2017 12:52 AM

Reply Helpful
Question marked as Solved

Nov 11, 2017 3:18 AM in response to jbh2 In response to jbh2

Probably the biggest issue is that folder actions don't run in the Script Editor, so it is difficult to use normal debugging techniques. What I do is separate the code that the action will perform from handers that call it, so that it can be run in different ways - for example:


# application double-clicked or script run from the Editor
on run
  doStuff for (choose file with multiple selections allowed) -- or choose folder, etc
end run

# items dropped onto the application
on open theFiles
  doStuff for theFiles
end open

# folder action - handle items added to a folder
on adding folder items to this_folder after receiving these_items
  doStuff for these_items
end adding folder items to

# do something with a list of file items
to doStuff for someFiles
  repeat with anItem in someFiles
    set anItem to anItem as text -- handle text, alias, POSIX, etc
    tell application "System Events" to set {theClass, theName} to {class, name} of disk item anItem
    if (theClass as text) is in {"folder", "«class cfol»"} then -- a folder (not a file package)

      log "folder:" & tab & theName -- do something with a folder

      tell application "Finder" to set folderItems to (items of folder anItem) as alias list
      doStuff for folderItems

    else -- a file

      log "file:" & tab & theName -- do something with a file

    end if
  end repeat
end doStuff

Nov 11, 2017 3:18 AM

Reply Helpful

Nov 11, 2017 8:15 AM in response to jbh2 In response to jbh2

I use a handler that allows me to provide a note to myself, but also the error number and message.


try

... some code to test that may be failing

on error errmsg number errnbr

-- handler name can be the just that, or some brief message about where I am in the code

my error_handler("main code section", errnbr, errmsg)

end try

return


on error_handler(handler_name, nbr, msg)

-- display an alert and then disappear after 10 seconds

return display alert handler_name & ": " & "[ " & nbr & " ] " & msg as critical giving up after 10

end error_handler

Nov 11, 2017 8:15 AM

Reply Helpful
User profile for user: jbh2

Question: How can I debug a folder actions applescript