Jump to content

Copying a Variable to clipboard?


Recommended Posts

I'm building a workflow, and as part of it, I need to copy a variable to my clipboard using Applescript

 

I'm using 

set the clipboard to {var:VARNAME}

and also tried

set the clipboard to "{var:VARNAME}"

But neither has worked.

 

Is it possible to grab the value of a varname and put it on my clipboard in this manner?

Link to comment

@mikejandreau  Could you share your workflow so far, so that we can help you further? 

 

You can save a variable for later use using an arg/var object:

https://www.alfredapp.com/help/workflows/utilities/argument/

 

Then you can include the saved variable using {var:varname} in your Copy to Clipboard output object:

https://www.alfredapp.com/help/workflows/outputs/copy-to-clipboard/

 

The item will then be copied to your clipboard as well if you need to use it outside of your workflow. :) If I've misunderstood what you're trying to achieve, please share the workflow and clarify what you've got in mind.

 

Cheers,
Vero

Link to comment

@Vero  I can't share the whole Workflow, as it has some company proprietary info in it right now.

 

But here's the "Run NSAppleScript" bit I'm working with, in its entirety:

 

on alfred_script(q)
tell application "Franz" to activate
tell application "System Events"
	keystroke "/away "
	delay 2.5
	key code 36
	key code 36
	delay 2.0
	keystroke "/status "
	set the clipboard to {var:away}
	tell application "System Events"
		keystroke "v" using {command down}
		delay 2.5
		key code 36
	end tell
	key code 48 using {command down}
end tell
if application "Spotify" is running then
	tell application "Spotify"
		pause
	end tell
end if
end alfred_script

Essentially, this bit of the workflow looks to see which Application is running (in my case, Franz), then sends some keystrokes to it to change the status. Then it goes on to pause Spotify.

 

Until now, I'd hard coded the "var:away" bit, but I am rewriting to share with my colleagues and want people to be able to set their own messaging there. Hence the variable. (I have another part of the workflow where they setup all the variables, which is working fine).

 

I suppose I could break this up and use the "Copy to Clipboard" Output in the workflow, but that could get messy with a bunch of different options I'm building in. But if it's the only way, I'm happy to give that a whirl.

Edited by mikejandreau
Corrected "Action" to "Output"
Link to comment

While we're on it, I also need to grab the "Franz" value from a variable in this line, too:

 

tell application "Franz" to activate

With it being AppleScript, it needs to be in quotes, but I can't figure out how to grab the value there and have it be in quotes to appease the AppleScript.

Link to comment

You'll need to switch to using a Run Script object set to /usr/bin/osascript (AS), then you'll have access to script environment variables. Alfred sets all workflow variables to the environment before running the osascript.

 

A simple example of getting at a workflow variable named 'away' into an AppleScript as var1 is as follows:

on run argv
  set var1 to system attribute "away"
  return var1
end run

In your case you'd be able to do the following:

set the clipboard to system attribute "away"

Cheers,

Andrew

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