Jump to content
jemostrom

Parameters to script/applescripts

Recommended Posts

Newbie here, I'm afraid I haven't really understood how data is passed between Alfred and scripts. Could anyone please explain how it's done?

 

I'm mostly interested in Applescript and Python (although I assume that all scripting languages works the same).

 

- jem

Share this post


Link to post

Newbie here, I'm afraid I haven't really understood how data is passed between Alfred and scripts. Could anyone please explain how it's done?

 

I'm mostly interested in Applescript and Python (although I assume that all scripting languages works the same).

 

- jem

 

Alfred passes data to script in the form of a placeholder variable, {query}. So, when passing data into a Run Script item with the language set to Python, you could do something simple like: 

temp = "{query}"
print temp

 

Which (obviously) would take the value passed into the workflow, save it into a variable named "temp" and then print that value. You can test this by creating a keyword, run script, and a large type item,  tie them together, and insert the code above. Anything entered after the workflow keyword would be displayed in Large Type.

 

The only exception to this would be the Run NSAppleScript item. NSAppleScript items differ from /usr/bin/osascript Run Script based items in, NSAppleScript items will block Alfred from doing anything else until the execution of the script completes. A Run Script item with the language set to osascript runs in the background. To use a Run NSAppleScript, you wrap your code in on alfred_script(q), similar to Alfred 1. See example below.

on alfred_script(q)
  set temp to q
  return temp
end alfred_script

 

Let me know if you have any other questions.

Share this post


Link to post

Let me know if you have any other questions.

 

Thanks! I've been looking for this simple explanation for ages. Nicely written.

 

I've now got my workflow up and running sweetly. I will post it soon, but there's one related question I have.

 

My workflow's embedded AppleScript contains a couple of variables that users will have to customise for their own purposes – is there a smart way to do that? Some way of prompting the user once at install and thereafter just working with the entered values.

Share this post


Link to post

Thanks! I've been looking for this simple explanation for ages. Nicely written.

 

I've now got my workflow up and running sweetly. I will post it soon, but there's one related question I have.

 

My workflow's embedded AppleScript contains a couple of variables that users will have to customise for their own purposes – is there a smart way to do that? Some way of prompting the user once at install and thereafter just working with the entered values.

 

I write my workflows in AppleScript too.

 

Check this workflow: http://www.alfredforum.com/topic/460-to-dropbox-30-formerly-move-to-dropbox-url-to-the-clipboard/

 

Or this working in progress with a more updated approach regarding a setup: download now

Share this post


Link to post

Thanks, but I'm finding that hard to follow without a basic explanation of the technique.

 

I have posted an example of how to Generate Feedback in Alfred 2 Workflows

 

From the Script Filter you can call the AppleScript with:

osascript xml.scpt "{query}"

To accept the query in the AppleScript, use:

on run argv
set query to argv as text
end run

Note that argv is a list and may cause problems if you don't convert it to text first.

Share this post


Link to post

Hi Carlos,

 

You can reduce this

set wf to load script POSIX file (POSIX path of ((POSIX file ((POSIX path of (path to me)) & "/..") as text) & "q_workflow.scpt" as text) as text)

to this...

set wf to load script POSIX file (POSIX path of ((path to me as text) & "::") & "q_workflow.scpt")

Share this post


Link to post

 

Hi Carlos,

 

You can reduce this

set wf to load script POSIX file (POSIX path of ((POSIX file ((POSIX path of (path to me)) & "/..") as text) & "q_workflow.scpt" as text) as text)

to this...

set wf to load script POSIX file (POSIX path of ((path to me as text) & "::") & "q_workflow.scpt")

 

Thank you. I’ll try it for sure. :)

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...