Jump to content
mikejandreau

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?

Share this post


Link to post

@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

Share this post


Link to post

@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"

Share this post


Link to post

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.

Share this post


Link to post

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

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