sindresorhus / Actions

⚙️ Supercharge your shortcuts
https://sindresorhus.com/actions
2.65k stars 115 forks source link

Action proposal: Is it possible to query Actions Global Variables outside of shortcuts? #129

Open theboyler opened 1 year ago

theboyler commented 1 year ago

Description

I'm writing an applescript to be called from within Apple Mail and I'd like to pull in some data from a Global Variable which a shortcut has set - I know I can call a shortcut via mail, which in turn can run an applescript, but if I want to do this without using shortcuts, can I query the value of a specific variable directly in applescript (or via command line?)

sindresorhus commented 1 year ago

I think the only way is with AppleScript. The global variables use NSUbiquitousKeyValueStore and there's no way to access that from the command-line. However, I don't have any plans to add AppleScript support. AppleScript is annoying to implement as it's very archaic.

zecanard commented 10 months ago

I’m going to assume that you’re not against using Shortcuts entirely, just that you don’t want to do your scripting in the Shortcuts app? If so, and provided you’re okay with creating a single helper Shortcut, you can invoke that Shortcut via AppleScript:

tell application "Shortcuts Events" to set value to run shortcut "Get Global Variable" with input "Key"
get value

And via the command line:

value=$(shortcuts run "Get Global Variable" <<< "Key")
echo $value

Then create a Shortcut that takes a Text input, retrieves the corresponding global variable, and Responds with the result. To retrieve a global variable of any type, use the Get all global variables Action, and interpret the result as a Dictionary.

Note: The parameter passed from the command line includes a trailing carriage return. So your Shortcut will need to Trim Whitespace on your Shortcut Input.

Screenshot 2023-12-22 at 18 09 53