How Do I Reference a Variable in an AppleScript?

Thank you, this works great. I for some reason though it would not accept that as a button but it did.

I just have one more simple question. How do I reference a variable in an applescript. I want to do something like this :

In the "Execute an AppleScript" action, click the "gear" icon, and select "Help".

About halfway down the Wiki page, there's a link that says "See Scripting for best methods". Click that.

You could have also found this by going to the Wiki, and searching for "AppleScript".

https://wiki.keyboardmaestro.com

The Wiki is your friend.

I am not quite sure why this doesn’t work though. You wrap the variable you want to pass in %, no?

With $ sign it does not work.

Also this macro, for some reason does not use my current clipboard, it used my last clipboard item, not current for some reason. :frowning:

From what the guide says, this should work :

But it doesn't. Nor does this work :

1 Like

Have you tried this simple solution:

2 Likes

That syntax isn’t for AppleScript. Look for the part that talks about AppleScript variables. It’s very clearly stated at https://wiki.keyboardmaestro.com/manual/Scripting.

This still does not work. :frowning:

That's what it passes into Dash.

Oops, never mind. I got it working now. Thank you Dan and Michael for your help and bearing with me for my silly questions. :slight_smile:

No problem. Please post your solution for the benefit of others. You can then check the “solved” checkbox so it will be clear in the top post.

Yeah, it is a very simple macro I needed to get around vim not behaving nicely with OS X selection. Here it is : search dash for selected text.kmmacros (20.5 KB)

I don't want to start another post as this is quite similar to what I was doing. This macro should take the text and do a query online with google and open the result in a new tab and bring focus to it. Here it is :

This however doesn't work, it doesn't open anything and I am not quite sure why.

How is this similar? Where’s the AppleScript?

yeah, you are right, I will start a new topic then

This is very strange. I believed it worked but then again it started not working. This doesn't do anything, it doesn't trigger Dash with the clipboard query. If I remove Current Clipboard variable and replace it with let's say 'hello' string, it does work. I really wish I can get this to work.

Here is how the macro looks for me :

then most likely the clipboard is not what you think it is.
Display the clipboard in a "Display Text in Window" Action before the "Open URL" Action.

I've been using this Action for over a year now, and it still works:

Yeah you were right, I set a delay and the clipboard is correct however Dash is still not triggering. This is really strange.

Can you tell me more about your action. What is the %Variable%, do you set it somewhere and what is the other text there for?

Thank you.

Perhaps a better approach is for me to give you a complete example of a Dash search. I just uploaded this macro.

Note that it contains some specific Actions to deal with selecting text in the Script Editor, so you may want to adjust those to your needs.

Hey Nikita,

Nyet.

Expansion of tokens doesn't work in Execute Script actions – you DON'T want unexpected things happening to your code.

--------------------------------------------------------------------------------
set baseGoogleSearch to "http://www.google.com/search?q="

set googleSearch to baseGoogleSearch & (the clipboard)

if class of googleSearch = text then
   open location googleSearch
else
   beep
end if
--------------------------------------------------------------------------------

If you really need to get a Keyboard Maestro variable in AppleScript then do this:

--------------------------------------------------------------------------------
set baseGoogleSearch to "http://www.google.com/search?q="

tell application "Keyboard Maestro Engine"
   set googleSearch to baseGoogleSearch & (getvariable "googleSearchString")
end tell

if class of googleSearch = text then
   open location googleSearch
else
   beep
end if
--------------------------------------------------------------------------------

-Chris

2 Likes

Thanks Chris, both of those AppleScripts were just what I was looking for to modify something else you had helped me out with in a different post. You seem to be helping me at every turn!