TFI TFI
TFI TFI

Reputation: 11

How to pass variable from AppleScript to Automator?

I'm looking for assistance with passing a variable from AppleScript to Automator.

I am working on a small automator app. I need to type a number that will be added to the end of a PDF's name. I used "Ask for text" in automator, however, I found out that, first, the pop-up window does not stay at the centre of screen. (27" iMac.), and second, every time after I input a number, I need to use mouse to click OK, the Enter key does not work.

So I turned to AppleScript for help. And here is what I found, a window that let me input the number, then click continue, then pass the number to Automator to add to PDF's name.

  1. Set Value of Variable to storage
  2. Run AppleScript: Ignore this action's input.
on run {input, parameters}

  display dialog "Please enter QC Round." default answer "" with icon stop buttons {"Cancel", "Continue"} default button "Continue"
--> Result: {button returned:"Continue", text returned:"MySecretPassphrase"}

  return input
end run
  1. Set Value of Variable to Continue
  2. Get Value of Variable to Storage
  3. Rename Finder Items: Add Text Add:_QC_Continue(Variable) after name

But it does not work. Can anyone help me fix it?

Upvotes: 1

Views: 1795

Answers (1)

red_menace
red_menace

Reputation: 3412

In Automator, an action gets its input from the previous action, and passes its results on to the next action. In a Run AppleScript action, the input is in the input argument to the run handler, and items returned by the handler are what get passed on.

In your example, you are just passing on the input, which doesn't have a value since the action is ignoring its input. The solution is to return the text from the dialog, for example:

on run {input, parameters}
   set dialogResult to (display dialog "Please enter QC Round." default answer "" with icon stop buttons {"Cancel", "Continue"} default button "Continue")
   return text returned of dialogResult
end run

Upvotes: 1

Related Questions