Jump to content

How to use multiple modifier in AppleScript record?


Recommended Posts

Hi, I create JSON from an AppleScript record via AppleScriptObjC. Names of single "mods"  are no problem (e.g. "ctrl" ), but it's not possible  to set "ctrl+cmd"  as name in an AppleScript record. Tried "ctrlcmd" and "ctrl_cmd" but that's not what Alfred needs.

 

Are there other ways to define ctrl+cmd that Alfred understands?

 

Here's the record:

set theQuery to "hello world"

set newRecord to {title:theQuery, arg:theQuery ¬
	, mods:¬
	{ctrl:{arg:theQuery, subtitle:"scope:selection", variables:{cleanQuery:"false", modifyQueryWith:"scope:selection"}} ¬
		, alt:{arg:theQuery, subtitle:"Anführungszeichen", variables:{cleanQuery:"true", modifyQueryWith:"quotes"}} ¬
		, cmd:{arg:theQuery, subtitle:"Entferne scope:", variables:{cleanQuery:"false", modifyQueryWith:"remove scope"}} ¬
		, shift:{arg:theQuery, subtitle:"Asterisks", variables:{cleanQuery:"true", modifyQueryWith:"asterisks"}} ¬
		, ctrl_cmd:{arg:theQuery, subtitle:"THIS DOESN'T WORK IN ALFRED", variables:{cleanQuery:"false", modifyQueryWith:"do something"}} ¬
		} ¬
		} as record

 

Here's the JSON (I'm adding Alfred's "{"items" " in the Script Filter, that's not the problem) :

 

[
  {
    "title" : "hello world",
    "arg" : "hello world",
    "mods" : {
      "ctrl_cmd" : {
        "arg" : "hello world",
        "subtitle" : "THIS DOESN'T WORK IN ALFRED",
        "variables" : {
          "cleanQuery" : "false",
          "modifyQueryWith" : "do something"
        }
      },
      "alt" : {
        "arg" : "hello world",
        "subtitle" : "Anführungszeichen",
        "variables" : {
          "cleanQuery" : "true",
          "modifyQueryWith" : "quotes"
        }
      },
      "ctrl" : {
        "arg" : "hello world",
        "subtitle" : "scope:selection",
        "variables" : {
          "cleanQuery" : "false",
          "modifyQueryWith" : "scope:selection"
        }
      },
      "cmd" : {
        "arg" : "hello world",
        "subtitle" : "Entferne scope:",
        "variables" : {
          "cleanQuery" : "false",
          "modifyQueryWith" : "remove scope"
        }
      },
      "shift" : {
        "arg" : "hello world",
        "subtitle" : "Asterisks",
        "variables" : {
          "cleanQuery" : "true",
          "modifyQueryWith" : "asterisks"
        }
      }
    }
  }
]

 

Edit: How can I use inline code in this forum? Backticks didn't work.

Edited by Pete31
Link to comment
4 hours ago, Pete31 said:

How can I use inline code in this forum? Backticks didn't work.

 

You can’t. Closest you can get is faking it. In MarkdownTransform, which I use for all my posts on this forum, I convert inline code (backticks) to:

 

[background=#eee][font=courier,monospace]CODE HERE[/font][/background]

 

Which when posted looks like CODE HERE.

Link to comment
4 hours ago, deanishe said:

I'm afraid I don't have any better advice than to use a language that has real JSON support.

 

I'm learning AppleScriptObjC as it's the right language for what I'm doing outside of Alfred. Also started to collect info for other languages. I've read that AppleScript is not what people use for their workflows and found a post where you said PERL is also not used much. What would be the recommended language for someone who knows nothing except AppleScript?

Edited by Pete31
Link to comment
3 minutes ago, Pete31 said:

What would be the recommended language for someone who knows nothing except AppleScript?

 

You may be able to transition to JXA (Apple has side-by-side examples). Since JXA is JavaScript, you’ll be able to output JSON.


But in truth you should begin to dive into a better, more supported, and more popular language, such as Ruby or Python (both good choices, and they ship with macOS).

Link to comment
6 hours ago, Pete31 said:

What would be the recommended language for someone who knows nothing except AppleScript?

 

What @vitor said. Ruby and Python are both very capable languages that you can use to build almost any kind of software, and they're included with macOS, which is important if you want to share your workflows.

 

At heart, they're both very similar. Ruby has a lot of syntactic "shortcuts" that Python doesn't, which means you can write more compact code. OTOH, having to write everything "long form" in Python makes it harder to write code you won't understand the next time you look at it.

 

I don't know about Ruby, but Python has bindings to macOS native APIs, so you can access Calendar or Contacts data relatively easily.

Link to comment
22 minutes ago, deanishe said:

having to write everything "long form" in Python makes it harder to write code you won't understand the next time you look at it.

 

That sounds very good! One thing I especially like about AppleScriptObjC is that everything is crystal clear when you read code, it's self-explanatory (once you've found what's needed …)

Link to comment
17 minutes ago, Pete31 said:

it's self-explanatory (once you've found what's needed …)

 

It is, but AppleScript is completely batshit in the world of programming languages. It makes very basic things extremely difficult to do, and it's not like any other language, so nothing you learn in AppleScript is transferable. That's why people who know other languages tend to hate it so much and use it only where absolutely necessary.

 

Generally speaking, time spent learning AppleScript is wasted time. For any common programming task, there are typically two ways to do it: (1) the way AppleScript does it and (2) the way every other language does it.

Edited by deanishe
Link to comment
3 minutes ago, deanishe said:

That's why people who know other languages tend to hate it so much and use it only where absolutely necessary.

 

Yeah I noticed that. It seems to be really hard to learn AppleScript if you already know any other language. I'm lucky as I started with AppleScript (and love it), now learning AppleScriptObjC and if I can't do something with it that I desperately want to do in Alfred then maybe I'll look into Python. However I don't think I'll do things I can't solve with AppleScript(ObjC).

Link to comment
48 minutes ago, Pete31 said:

I'm lucky as I started with AppleScript

 

Hmm. That sounds a lot like arguing you're lucky to have spent your entire life in Gelsenkirchen because you have no idea what a hole it is…

Edited by deanishe
Link to comment
1 minute ago, deanishe said:

That sounds a lot like arguing you're lucky to have spent your entire life in Gelsenkirchen because you have no idea what a hole it is…

 

Well, if almost all you want to do is scripting apps then AppleScript is the way to go. Of course there's JavaScript but it happened that it's AppleScript for me and there simply wasn't a need for any other language. I also don't want to miss UI scripting when there's no other way. AppleScript does things you can't do with any other language  (maybe JavaScript is capable of UI scripting too, don't know).

Link to comment

 

2 hours ago, Pete31 said:

it's AppleScript for me and there simply wasn't a need for any other language.

 

You're importing and using a bunch of Objective-C classes to handle all the stuff AppleScript can't do. That's not really AppleScript any more.

 

2 hours ago, Pete31 said:

AppleScript does things you can't do with any other language

 

You can do all of those things via JavaScript or Swift or Objective-C (and by extension any other language that has the necessary Objective-C bindings, like Python).

 

I don't know if you're aware, but importing huge libraries like Foundation into AppleScript is extremely slow. This is how long outputting some simple JSON takes in various languages:

 

0.216s	AppleScript
0.033s	Python
0.024s	JavaScript
0.005s	Go
0.004s	Objective-C

 

That's basically all start-up time, so it doesn't much matter in a long-running application, but using Objective-C libraries via AppleScript, instead of natively, ruins any chance you have of writing a workflow that performs well.

 

It's entirely subjective, of course, but I couldn't look at those results and not rewrite in a different language. It's exactly things like this that have always led to me learning another language (because it was an obviously better choice).

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

I don't know if you're aware, but importing huge libraries like Foundation into AppleScript is extremely slow.

 

I'm aware. I totally understand what you're telling me, however keep in mind that I'm writing my first workflow that does more than opening a URL in a new Safari window or initiating a DEVONthink search. Doesn't make sense to immediately start to learn another language just because I want one more "advanced" workflow (speaking as a bloody beginner). I can see how you and others repeatedly speak against AppleScript and it's definitely right to warn beginners so they don't get on the wrong track when they mainly want to write Alfred workflows. However I'm writing AppleScript for some years now, there's seldom a day where I don't write for me or someone on a forum. Starting another language at this point would simply be insane, especially as I already started with AppleScriptObjC. The time for another language will come and then I'll choose one that works well with Alfred but first I'll get familiar with AppleScriptObjC as that's what makes sense to me. There's not only Alfred.

Link to comment
6 minutes ago, Pete31 said:

There's not only Alfred.

 

And that’s exactly why you should learn another language. AppleScript, besides being a poor language with one redeeming quality (UI automation), works in a single operating system (macOS), has bad documentation (like all other Apple technologies), is unpopular (which means fewer avenues to get help when stuck), is neglected and will die (Apple hasn’t worked on it for years, dismantled the division responsible for it, and no longer adds support for it in their own tools).


In sum, you’re investing time in a dying technology. Worse, because AppleScript is atypical, knowledge from it doesn’t transfer well to other languages, so you’re making it harder to switch when you need to. Ruby and Python aren’t “good for Alfred”, they are good in general, well supported, thriving, cross-platform, and what you learn from them can be applied to other languages.


Learning another language doesn’t mean completely abandoning AppleScript, it just means you should start to become familiar with something else. At the very least you should transition from AppleScript to JXA: most features are there, in terms of UI automation it’s more flexible and can do more, and in the end it’s JavaScript, an insanely popular language whose skills will serve you in a variety on areas, including making apps.

 

22 minutes ago, Pete31 said:

Starting another language at this point would simply be insane

 

Quite the contrary, investing more in something that is dying is the insane part. Again, the company responsible for it—and the only ones who can keep it alive—no longer use it themselves and fired the ones responsible for it. It’s hard to get any clearer that is has no future.

 

The sanest thing you can do in this regard is to start learning another language now, while you have the time to do it slowly.

Link to comment
3 minutes ago, vitor said:

And that’s exactly why you should learn another language.

 

4 minutes ago, vitor said:

because AppleScript is atypical, knowledge from it doesn’t transfer well to other languages, so you’re making it harder to switch when you need to.

 

4 minutes ago, vitor said:

Learning another language doesn’t mean completely abandoning AppleScript, it just means you should start to become familiar with something else.

 

4 minutes ago, vitor said:

At the very least you should transition from AppleScript to JXA

 

First off you didn't tell me anything new about AppleScript, I'm well aware. It's not meant disrespectful, however as a new (active) forum  member I'm quite surprised how massive people here are trying to force something upon me which I repeatedly said I neither need nor plan to do in the near future. I'm regular on several forums and never experienced something like this. How clear do I have to say:

 

I love AppleScript.

I write almost every day.

I don't want to learn any other language at this point, except AppleScriptObjC.

 

Gentlemen that's really not how new members should be treated. If I were to start to script and didn't know anything about it then it might be ok, however I'm quite experienced in AppleScript and told you so.

 

There's no reason for me to start a new language as AppleScript won't die, believe me.

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