Calling variable from automator in AppleScript - applescript

I am struggling to call the 'folder' variable within my apple script (shown in the image).
I don't have much experience with apple script so I'm sure I'm not calling it correctly but I can't find a better example online.
Any help would be appreciated.
AppleScript

You don't need a variable action.
The result of the first action is passed to the second as a list of alias specifiers.
item 1 of input is the desired item, you just have to coerce it to POSIX path

Related

NSUserAutomatorTask variables array/list values cannot be parsed by Automator actions

I am using NSUserAutomatorTask to launch an Automator workflow from a macOS app.
I'm passing in variables via the variables property:
https://developer.apple.com/documentation/foundation/nsuserautomatortask/1418099-variables
Inside the Automator workflow I will then use Get Value of Variable actions to grab the variables that were set on the NSUserAutomatorTask and pass the variables to subsequent Automator actions:
Code looks like this: (simplified; I also check that the variables exist in the workflow)
let workflow = try! NSUserAutomatorTask(url: url)
workflow.variables = [
"singleFilePath": "/image1.png",
"multipleFilePaths": ["/image1.png", "/image2.png"],
]
workflow.execute(withInput: nil)
I'm able to print out the variable values in an alert via an Ask for Confirmation action:
The String variable's value is a simple string: /image1.png
The [String] array variable's value is wrapped in parentheses and each item is quoted:
(
"/image1.png",
"/image2.png"
)
I now run the pictured Automator workflow that first gets a variable's value and then attempts to open those Finder items.
The singleFilePath var works. It's passed on to the Open Finder Items action, and that file is opened by the default application.
Passing multipleFilePaths in the same manner does not work. No files are opened. Automator displays the error:
The action "Open Finder Items" was not supplied with the required data.
The action is "Open Finder Items", so there must be some way to pass multiple file paths to this action.
My questions and solutions in order of preference are:
Why is the default variable array/list format not working when passed to the subsequent action? Is there any way to pass or parse the array variable in a compatible format?
Can we use a single Run AppleScript action to reformat the array variable into a format that can be passed to subsequent Automator actions? (I'd like to continue to chain Automator actions rather than run pure AppleScript).
Open Finder Items actions can open an array of items if used in a normal non-variables workflow. And it's seemingly in the exact same format.
As a control to test this, rather than the variable, I'm using the Get Specified Finder Items action.
I then use a View Results action to inspect what is sent to Open Finder Items.
The results are seemingly exactly the same as when I parse the variable:
(
"/image1.png",
"/image2.png"
)
This workflow does correctly run the final action and open the files.
So Open Finder Items should work with the data I'm setting in the variable. I'm unsure why the variable data cannot be opened but manually-picked files can.
I opened a DTS ticket for this issue and received the following response:
I’m sorry to say the engineers working on this have confirmed that yes, it’s a bug, and no, there isn’t a workaround for sandboxed apps. They’ve also noted this isn’t a regression in behavior from previous releases.

How to copy all the info from clipboard via AppleScript

Here is the thing:
I'm using AppleScript in Automator to get the clipboard value, and of course it works, but when I want to get multiple separated value, it always returns me only one value on the top。
Here is the step:
In Automator, import multiple "Get Value of Variable" actions, and
in these actions, I will set multiple values, all of these values
are e-mail format
Import an action named “Ask For Confirmation”, without this action,
I can’t pass multiple values to the next action “Choose from list”(I
don’t know why, but it works)
Import an action named “Choose from list” to let users choose the
e-mail values I’ve pre-set in this Automator application
Import another action named “Set Value of Variable” to get the
values users have chosen
Import an action named ”Copy to Clipboard” to copy these values to
clipboard
Import an action named “Run AppleScript” and here is my code:
on run {input, parameters}
--get the clipboard info
set Storage to get the clipboard
display dialog Storage
return input
end run
I've tried to copy some text_1, text_2 ... manually(command+c, command+v) and then run my AppleScript only, and it turns out the result what I really want like this:
Here is my Script Editor code:
I have to say, due to some limitation I can only use Automator and AppleScript,so is there any solution or suggestion?
Here is the "Get Value of Variable" picture
Get Value of Variable
Possible Explanation:
I believe this is a bug in either the Automator Copy To Clipbard action or AppleScript. Automator actions are often written in Objective-C, and it has some data types that AppleScript doesn't. It looks like the Automator action copies an array to the clipboard, which is something you can do with Objective-C, but not with AppleScript.
My feeling is that AppleScript is the entity at fault here, as the action is doing what it is meant to, and within the Automator context, it wouldn't pose a problem keeping the clipboard's data as an array type. AppleScript likely hasn't catered for this in its implementation of clipboard data handling, and does a poor job of coercing the array or list into plain text, which—as you stated—only contains the first element of the array.
Solutions:
1. Use a do shell script command
Instead of:
set Storage to get the clipboard
try:
set Storage to do shell script "pbpaste"
2. Use AppleScriptObjC
Since the Automator action is probably written in ObjC, it's reasonable to assume that using AppleScriptObjC will give us access to the necessary data types.
Replace your entire AppleScript with this:
use framework "Foundation"
use scripting additions
set Storage to (current application's NSPasteboard's generalPasteboard's ¬
stringForType:(current application's NSPasteboardTypeString)) ¬
as text
display alert Storage
3. Access the data through the input variable
The Run AppleScript action in Automator takes the result of the previous action and stores it in the variable attached to the on run {input, parameters} handler, namely input (you can ignore parameters).
Currently, your workflow actually sends the contents of the clipboard (the output of the Copy To Clipboard action) directly to the input variable of your AppleScript.
Therefore, you can replace the entire AppleScript with this:
on run {input, parameters}
set the text item delimiters to linefeed
set Storage to the input as text
display dialog Storage
end run
Any one of these solutions should work, so just choose your preferred method. Number #3 probably makes most sense in your current set up, and is the simplest.

Mac Automator/AppleScript: Is there a way to refer to Variables within AppleScript?

I've seen a lot of questions about this on the interwebs but no answers. Is there a way to refer to an Automator 'variable' within AppleScript? I'd like to do some string manipulation as part of a workflow. I've worked around this by using Get Variable and passing them into temporary files, but it's kind of ugly.
I was trying the same ting as Steven. My conclusion is that when you run a flow inside the "Automator" application then your applescript can access Automator-varaibles via the Apple Script "Automator Suite" interface. For example:
set my_variable to value of variable "The Variable" of workflow 0 of current application
display dialog my_variable as text
set my_variable to "Test"
But if you save the flow as a stand alone application then it does NOT include the "Automator Suite" into the application and therefore the above script will no longer function :-(
An AppleScript used in a workflow accepts two parameters: input, or the output of the previous workflow, and parameters, the options set in the workflow's UI (if applicable). If the string you are manipulating is part of the workflow's input, it will be in input.
More information is available here.

Pass variable and its contents from workspace to GUI function in MATLAB

I have a variable in the MATLAB workspace and I want to pass the variable name and its contents to a function in my GUI.
How do I achieve this task?
I'm not totally sure what you mean when you say "pass the variable name and its contents", but here's one possible solution. After you pass a set of data to a function like so:
some_function(data); %# Pass the variable "data" to a function
You can get the variable name of the input argument from inside the function using INPUTNAME:
function some_function(inputArgument)
name = inputname(1); %# Will return "data" as the name of the input variable
end
EDIT: As pointed out in a comment by High Performance Mark, the variable inputArgument inside the function will contain the values stored in the variable data in the workspace of the caller.
If this question is related to your other most recent question, then why not build the operation into your GUI? You can use guide to create a pushbutton, and place the code under the callback function.
I am assuming that you have created the figure with GUI using the GUIDE, and that you know the 'Tag' names of the GUI objects.
((1)) Open the figure using the GUIDE, ((2)) Open the Property Inspector for the figure (select the background, the light-gray gridded area of the figure, and double-click over it, to make the Property Inspector for the figure to pop-up), ((3)) Turn the 'HandleVisibility' 'on' (by default, it may be set as 'callback'), ((4)) Save the figure and close the GUIDE, and finally ((5)) set the GUI property values from the MATLAB Console (or "Command Window") using some parameters that are currently available on your workspace.
I hope this helps.
Best,
Y.T.

capturing what keys were used to launch vbscript

I have an application that has 'macro' capabilities. When I map some keys on the keyboard to perform the 'macro', I can also have it launch vbscript instead.
What i'd like to try and do is within my vbscript figure out what keys were used in order to launch the script. Is it posible to do this? Could there be a way in vbscript to figure out what keys were last touched on the keyboard and then I could apply my logic.
The purpose of doing this is to keep the code in a single .vb file instead of several seperate .vb script files(one for each keyboard mapping, possible 3-4). Obviously we are looking to just maintain 1 file instead of multiple files with essentially the same code in each one.
I am leaning towards the idea that this is not possible, but i figured this would be a worthy question for the masses of StackOverflow. Thanks for the help everyone!
What you are asking for is not possible.
Can you change your VBScript to accept parameters and then call it with a different parameter based on which hotkey was selected?
I agree with aphoria, the only way to make something like this possible is if your keyboard mapping software allows you to assign a script/command with parameters/arguments. For example if you used
c:\Temp\something.vbs
then you would change this to
%WINDIR%\system32\wscript.exe c:\temp\something.vbs "Ctrl-Alt-R"
Then in your vbscript code you could collect the argument using the wscript.Arguments object collection to do actions based on what argument/parameter was passed. See the following two links for more info:
http://msdn.microsoft.com/en-us/library/z2b05k8s(VS.85).aspx
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0915.mspx
The one possible approach you may use is to install keylogger and read its log in your VBScript.
For example save script start time in the very beginning of the script
StartTime = Timer()
and then read one log record of your keylogger before this time.

Resources