The early part of this morning was spent creating a means of adding an entry to Captain's Log from Safari without leaving Safari. It could have been an all-AppleScript solution, but I wanted to try Automator first.
Some of the feedback I get is pretty negative on Automator, but of ShortCuts, AppleScript and Automator, the most approachable, to me, is Automator. Perhaps it's just because it looks familiar since it's been around so long.
Anyway, I figured I'd make a Quick Action (Service) that would capture the URL of the page I happened to be in, re-use the AppleScript for getting the entry title and the entry text, and finally making the log entry itself. So most of the action takes place in an AppleScript in Automator.
Automator meant I didn't have to "tell Safari" and deal with its object model. There's an Automator action that does exactly what I want, "Get URL."
I followed that with a Set Value of Variable action. I thought I could pass the variable directly into the AppleScript in Automator. The variable in "Set Value.." was "pageURL," and I thought I could just use that in a "tell Tinderbox..." statement.
The workflow worked up until the AppleScript, where it complained that the variable was undefined. So I asked ChatGPT if I could pass a variable from a Set Variable action in Automator to an AppleScript in Automator.
To pass a variable set in Automator to an AppleScript action in Automator, you can use the "Set Value of Variable" action to store the variable value and then access it in the subsequent AppleScript action. Here's how you can do it:
1. Add a "Set Value of Variable" action in your Automator workflow and set a variable name and value.
2. Add an "Run AppleScript" action after the "Set Value of Variable" action.
3. In the AppleScript action, you can access the variable set earlier using the following syntax:
```applescript
set myVariable to (variableName of variables)
```
Replace `variableName` with the name of the variable you set in the previous action.
By following these steps, you can pass a variable set in Automator to an AppleScript action within the same Automator workflow.
That suggested to me that I needed to create the variable that would ultimately be the one Tinderbox used to set the $URL attribute early in the AppleScript by setting to the value of the variable from the preceding Automator action.
Welp, that didn't work either. Undefined.
I figured I'd give the chatbot another chance. Here's its answer:
In your AppleScript action in Automator, you need to access the variable value set in the previous action correctly. Here's how you can modify your AppleScript to retrieve the variable value and assign it to `theURL` variable:
```applescript
on run {input, parameters}
set theURL to item 1 of input
-- Your AppleScript code using theURL variable goes here
return input
end run
```
By using `input` in the AppleScript action, you can access the variable value passed from the previous action. Make sure to adjust your AppleScript code accordingly to utilize the `theURL` variable correctly within the script.
And that worked.
I can't say I understand exactly why it worked, but I'm happy it worked. I've got a few ebooks on AppleScript and Automator and at least it's given me some clues to look for using Search. Might make things a little clearer.
Originally posted at Nice Marmot 09:38 Wednesday, 13 March 2024