Jump to content

Pass clipboard to Applescript in Alfred


Recommended Posts

I know this is a total noob question, but how do I pass the contents of the clipboard to an Applescript that I want to trigger in Alfred?

I have a script that works fine if I copy some text to the clipboard and then run the script. Now that it works, I would like to be able to trigger the script from Alfred. I just cannot work out how to pass the contents of the clipboard to the script, the first line of which is `set theText to the clipboard`.

Is there a way to select the text in the document and then trigger the script? I suppose that would mean changing the first line, but that's no problem.

I'm happy to read a tutorial, but I'm just not sure where to look, as this seems to basic.

Thanks

Jeremy

Link to comment

Sorry, it's not entirely clear what you're asking. If the first line of your script is "set theText to the clipboard", it should work. You're not running it using a Run NSAppleScript action, are you? If so, don't: it behaves very strangely. Use a normal Run Script action instead, with Language = /usr/bin/osascript (AS).

Link to comment
1 hour ago, jeremycherfas said:

I guess I don't know enough about Applescript to dislike it as much as many people here.

 

You posted a single line of the script and asked us to explain why the script doesn't work. All anyone can do in that situation is guess what the problem might be, regardless of which language it's written in.

Link to comment

I'm sorry. My tone came over wrong. I sincerely meant thank you. I tried it as a Run Script action instead and all is well.

My comment reflected the fact that whatever I searched for in Applescript seemed to focus more on why Applescript was a bad language rather than how to use it with Alfred. Your tip about not using NSAppleScript action, for example, cropped up occasionally without context.

Link to comment
8 hours ago, jeremycherfas said:

I'm sorry. My tone came over wrong. I sincerely meant thank you.

 

No problem. I wasn't offended or anything.

 

Please remember to post the script or workflow you're working on next time you have a question: it's almost impossible to answer questions about it otherwise.

 

8 hours ago, jeremycherfas said:

whatever I searched for in Applescript seemed to focus more on why Applescript was a bad language rather than how to use it with Alfred.

 

Yeah. You'll often get an answer to your AppleScript questions, but griping about the language is typically included for free because it was a PITA to get it working. Also, AppleScript doesn't support JSON, which is pretty important when writing workflows. Hence the many "don't use AppleScript if you don't have to" posts.

 

There are some posters who are really, really good at AppleScript, but they don't answer many questions, unfortunately.

 

8 hours ago, jeremycherfas said:

Your tip about not using NSAppleScript action, for example, cropped up occasionally without context.

 

That's because none of us actually understands why some scripts work with NSAppleScript and some don't…

 

In any case, it's a good idea to avoid it because Run NSAppleScript actions are run on Alfred's own main thread, so Alfred is unresponsive as long as the script is running. You don't have that problem with regular Run Script actions. There are very few upsides to using it, and I think it's only still there for backwards compatibility.

Edited by deanishe
Link to comment
2 hours ago, jeremycherfas said:

Then I could study their workflows and learn.

 

You shouldn’t be striving to learn AppleScript if it’s one of your first languages:

  • It’s underpowered.
  • Has few people using it, thus few resources to learn from (and a bunch of examples are plain inefficient).
  • It’s not updated anymore.
  • It has a good chance of being deprecated soon.
  • It’s so different from other programming languages that what you learn from it won’t be transferrable to other languages.

You should concentrate on AppleScript as a “get in, get out” language, using it only for communicating with apps. If you still want to learn it, go with JXA (JavaScript for Automation) instead. It can do about the same (a bit more in some areas, a bit less in others, but you gain more than you lose) and it’s JavaScript, so you’ll be learning something useful.


With all that out of the way, resources:

Alfred-specific:

  • I think @Carlos-Sz used to make AppleScript Workflows. Though that was before Script Filters even supported JSON, if I recall correctly, so they might be old and your mileage may vary in recent versions of Alfred.
  • PlayAddress (disclaimer, I’m the author) is short, made mostly with JXA, and shows both how to use JSON in Alfred Script Filters and how to read the clipboard (and how to deal with errors that happen when doing so). It also has code to get information from web browsers, which is a nice example why JXA is nicer to use than AppleScript.

Link to comment

A lot depends on what you’re trying to do.  For some apps (e.g. DEVONthink, BibDesk, FileMaker, the iWork and MSOffice suites, etc.) AppleScript lets you automate stuff you couldn’t do otherwise.  But for a lot of tasks, I agree, it’s not the most suitable language.

Link to comment
2 hours ago, dfay said:

AppleScript lets you automate stuff you couldn’t do otherwise.

 

That's what Vítor meant by a "get in, get out language". You just use AppleScript (or preferably JXA) to get data out of the application and put data and commands back in. Everything else you do in a more sensible and/or capable language. Even if you like JS, JXA is a PITA because of its spartan runtime. A lot of the time, you end up importing a bunch of native libraries and writing some weird Objective-C/JavaScript mashup.

Edited by deanishe
Link to comment

My "problem" is that I started using Applescript long before I had even heard of Alfred, and so for some of those simple in and out automations it is just the easiest thing for me. I'm not looking for help with Applescript (well, I am ...) but for help in using it with Alfred, mostly using Alfred as a trigger.

 

From what I hear (e.g. On Automators FM) JXA is even stranger.

Link to comment
43 minutes ago, jeremycherfas said:

From what I hear (e.g. On Automators FM) JXA is even stranger.

 

It's an absolute horror show as far as the AppleEvents APIs go—and straight-up doesn't work with many apps—but for other stuff JS is a much more capable language than AS. Especially generating the JSON that Alfred often wants.

 

44 minutes ago, jeremycherfas said:

help in using it with Alfred, mostly using Alfred as a trigger.

 

Right you are. In that case, "Don't use Run NSAppleScript" is probably all you need to know.

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...