I am working on a script and encountered a strange issue: the whole script is doing what it’s supposed to do from external script editor but stops functioning when it is saved under the DT’s toolbar script folder and run as a button script.
I narrow the issue down to a recursive handler that obtains all RTF/RTFD/markdown files within any selection of files/groups/smart groups. The test script (see below) successfully retrives all files within a selection in any external editor or when it is saved under the DT’s script menu folder BUT do not function (when the selection contains at least one group) when it is saved under DT’s toolbar script folder. The alerts “Problem !” is shown every time the script is triggered by a button - the “getAllChildren” handler does not return any records.
Thank you very much in advance!
The testing script:
-- select any >1 items that include at least one group or smart group
tell application id "DNtp"
set theGps to selection
set theRecords to my getAllChildren(theGps)
if length of theRecords is 0 then
display alert "Problem !"
else
display alert "Success !"
end if
end tell
on getAllChildren(theseGps)
local l
set l to {}
tell application id "DNtp"
repeat with each in theseGps
if (type of each as string) is in {"group", "smart group"} then
set l to l & my getAllChildren(children of each)
else
if (each's type) is in {rtfd, rtf, markdown} then set end of l to each's item 1
end if
end repeat
end tell
return l
end getAllChildren
P.S. I used each's item 1 instead of each when using repeat with each in ... as a trial-and-prove approach. Sometimes, when the variable is a selection (e.g. Set a to selection) repeat with each .. won’t work unless I use each’s item 1.
The testing script returns 0 records even when I use each instead - when running from the button.
if (each's type) is in {rtfd, rtf, markdown} then
set end of l to each's item 1
do shell script "echo " & (name of each's item 1) & " >> ~/desktop/001.txt"
end if
Running the script from Script Editor and from the toolbar button yield different results.
No wonder. I tried using string value for comparison (each's type as string) is in {"rtf,"rtfd","markdown"}, and also tried using two-steps (get all children regardless of the file format from a simplified handler, then screen the format in a loop) and the methods still doesn’t run in toolbar menu. Now I know that the issue is on the comparison statement!
I will try to use individual conditional statement instead … (each's type is rtfd) or (each's type is rtf) or (each's type is markdown).
That’s the issue mentioned in a different thread few days ago, comparing the type after converting it to a string is more reliable in case of text, Markdown or smart groups.
No, the test script based on string value comparison still returns "Fail " message when running in the toolbar folder.
This was what happened. The testing script would fail or success randomly at the first trial (first time saving in the toolbar folder or when it is opened in editor and recompiled). However, if DT is restarted, the button script will fail every time again.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application id "DNtp"
set theGps to selection
set theRecords to my getAllChildren(theGps)
display alert length of theRecords
if length of theRecords is 0 then
display alert "Problem "
else
display alert "Success"
end if
end tell
on getAllChildren(theseGps)
local l
set l to {}
tell application id "DNtp"
repeat with each in theseGps
if (each's type as string) is in {"group", "smart group"} then
set l to l & my getAllChildren(children of each)
else
if (each's type as string) is in {"rtf", "rtfd", "markdown"} then set end of l to each's item 1
end if
end repeat
end tell
return l
end getAllChildren
All rtf/rtfd/markdown formats. My if condition returns “Problem!” when the handler returns 0 records. I test it with selection of files in single format and in multiple formats.
Perhaps there is another way to write the handler?
It’s OK and don’t spend too much time on the issue, because now I know that the issue exists and is unrelated to 3rd party additions.
I will sort out alternative for the same function. The script doesn’t have to be run as button script.
Thank you for confirming the issue and now I know which area to debug.
Finally, the handler works by changing this line:
if each's type is group or each's class is smart group then
set theRecords to my getAllChildren(theGps, {rtf, rtfd, markdown})
on getAllChildren(theseGps, theFormat)
local l
set l to {}
tell application id "DNtp"
repeat with each in theseGps
if each's type is group or each's class is smart group then
set l to l & my getAllChildren(children of each, theFormat)
else
if each's type is in theFormat then set end of l to each's item 1
end if
end repeat
end tell
return l
end getAllChildren