When i have a result from an applescript as a list i can easily export is as a text into a variable when i choose Save As Variable on the bottom of the action.
a list like {item1, item2, item3} will then be exported as KM variable: item1, item2, item3
But when i want to export multiple lists as result i have to use tellapplication "Keyboard Maestro Engine" tosetvariable "KMvariable" to ASvariable
But then i first have to convert it to a string and then all the items are not separated anymore.
a list like {item1, item2, item3} will then be exported as KM variable: item1item2item3
As solution I can, within AS, do a repeat action for each item in a list and then split the items with ", " but i have found that it is slowing down the script noticeably.
Is there a more elegant way to export multiple lists as multiple KM variables?
My example:
using the showJSON function requires the use framework "Foundation" incantation at the top, and
a call to showJSON within the scope of tell application ... requires the my prefix.
Expand disclosure triangle to view AppleScript Source
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
on run
tell application "Keyboard Maestro Engine"
setvariable "allButtonsList" to my showJSON({"Alpha", "Beta", "Gamma"})
end tell
end run
-- showJSON :: a -> String
on showJSON(x)
set c to class of x
if (c is list) or (c is record) then
set ca to current application
set {json, e} to ca's NSJSONSerialization's dataWithJSONObject:x options:1 |error|:(reference)
if json is missing value then
e's localizedDescription() as text
else
(ca's NSString's alloc()'s initWithData:json encoding:(ca's NSUTF8StringEncoding)) as text
end if
else if c is date then
"\"" & ((x - (time to GMT)) as «class isot» as string) & ".000Z" & "\""
else if c is text then
"\"" & x & "\""
else if (c is integer or c is real) then
x as text
else if c is class then
"null"
else
try
x as text
on error
("«" & c as text) & "»"
end try
end if
end showJSON
Don't know if this is what you mean but I use this handler to convert lists into strings
(*
on convertListToString(theList, theDelimiter)
- returns a text string representation of theList where
- each list element is separated by theDelimiter in the string
*)
on convertListToString(theList, theDelimiter)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theString to theList as string
set AppleScript's text item delimiters to prevTIDs
return theString
end convertListToString
ok, that is something i can read, almost. I played a bit with this one.
One thing my basic knowledge is still lacking, where is the output of the handler? I mean, how or where can i set a variable to its output?
on run
tell application "Keyboard Maestro Engine"
setvariable "allButtonsList" to my convertListToString({"Alpha", "Beta", "Gamma"}, ",")
end tell
end run
(*
on convertListToString(theList, theDelimiter)
- returns a text string representation of theList where
- each list element is separated by theDelimiter in the string
*)
on convertListToString(theList, theDelimiter)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theString to theList as string
set AppleScript's text item delimiters to prevTIDs
return theString
end convertListToString
It sets the KM variable allButtonsList to Alpha,Beta,Gamma