Jason Wang
Jason Wang

Reputation: 23

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:

  1. 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
  2. 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)
  3. Import an action named “Choose from list” to let users choose the e-mail values I’ve pre-set in this Automator application
  4. Import another action named “Set Value of Variable” to get the values users have chosen
  5. Import an action named ”Copy to Clipboard” to copy these values to clipboard
  6. 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

Here is the whole actions picture

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: AppleScript Result

Here is my Script Editor code: 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

Upvotes: 1

Views: 3407

Answers (1)

CJK
CJK

Reputation: 6092

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.

Upvotes: 2

Related Questions