Jump to content

Passing three variables to AppleScript: works in script editor, not in Alfred


Recommended Posts

I know that I shal be slated for using AppleScript but, braving the comments, here is the problem.

 

First, here is the test workflow on transfer.sh. Now the detailed explanation…

 

I have an basic Apple Notes workflow (is any Apple Notes workflow "basic”?) simply to display a selected note from a pre-populated list. The problem is getting Notes to look in the correct place for the note. I have an AppleScript script which works perfectly in Script Editor but which fails in its attempt to split the variables in Alfred.

 

By way of example, the List Filter has an item "Check list”—with the arguments "Check list,On My Mac,Local notes”. The AppleScript in Alfred is as follows:

 

on run argv
set astid to ""
set AppleScript's text item delimiters to "," -- Use "." as separator
set argv to text items of argv -- This splits the string into a list, using the new separator
set theNote to item 1 of argv
set theAccount to item 2 of argv
set theFolder to item 3 of argv
tell application "Notes"
    tell account theAccount
        tell folder theFolder
            show note theNote
        end tell
    end tell
end tell
set AppleScript's text item delimiters to astid
end run

 

In Alfred the script fails with the error execution error: Can’t get item 2 of {"\"Check list,On My Mac,Local notes\""}. (-1728). However, if I run the script in Script Editor (removing the run commands and setting argv to Check list,On My Mac,Local notes) it works.

 

Am I missing something obvious?

 

Stephen

Link to comment

You’re getting the arguments (argv) as a single argument with commas and then splitting argv into itself inside the code. Let’s simplify that. The “proper” method is to send them as separate arguments from the start. Do that by sticking a Split Arg Utility between the List Filter and Run Script. Set it to Split with: Comma and Output as: Arguments. Then, in your code all you have to do is delete lines 3 and 4.

Link to comment

It’s great to see you go with the code route. And for Notes as well!


Keep this idea of “arguments” in mind. In practice, it means understanding why function one two is different from function "one two" and that those are a dichotomy: when one of those is required there will be a right one and a wrong one.


It’s a concept whose understanding “levels up” your programming skills. You get there with practice. Each environment has their own rules but they aren’t that many and are usually well defined. In the example above, it may help to realise that function one two is the same as function "one" "two". Written that way, it’s perhaps simpler to see how that is different to function "one two": the former has two arguments (the first being the word one and the second the word two, without spaces) while the latter has one argument (one two as a single argument, space included). Now if I say that function " one" "two" is different from all the other examples, why is that? Answer: there is a space in the first argument.


You can typically transform one into the other, but joining multiple arguments is simpler than splitting a single one, as the contents of your single argument may be tricky to parse. Striving to keep multiple arguments separate is, then, desirable.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...