Applescript to copy in app A and paste in app B without leaving app A?

Hi, I'm a completly begginer in Automator and AppleScript.

Somehow I've managed to do this wonky service to copy selected text on app A, convert it to plain text, paste on app B and jump back to app A.

However, the process of going from an app to the other and then back is very, very distracting, a total anoyance when I'm busy reading a text (I use all apps in full screen with spaces, so you can guess how annoying it is).

I don't even know whether that is possible, but could anyone help me to make the same thing, without ever leaving the app A (which must be on full screen)? Or at least make it look better, maybe?

Here is the AppleScript:


on run {input, parameters}


tell application "System Events"

keystroke "c" using command down


do shell script "pbpaste | pbcopy"

end tell


delay 0.2


tell application "AppB" to activate

tell application "System Events"

tell process "AppB"

keystroke "v" using command down

end tell

end tell


delay 0.5


tell application "System Events"

tell process "Finder"

activate

keystroke tab using command down

end tell

end tell



return input

end run


Thank you

MacBook Pro (13-inch Mid 2009), OS X Mountain Lion (10.8.5)

Posted on May 28, 2014 7:10 PM

Reply
4 replies

May 28, 2014 9:55 PM in response to sanfm12

Obfuscating AppA and AppB like this doesn't help people provide a direct answer.


If you avoid UI scripting (as Red_ mentions) you might be able to do this, depending on the applications in question.


Most apps provide some degree of direct control via AppleScript, meaning you might be able to get the selection in AppA whether or not it's the frontmost app. Likewise you might be able to insert text into AppB without it being frontmost. Every app is different, though, and without knowing the apps in question it's impossible to be more specific.


So what are the apps? Are they scriptable?

May 30, 2014 12:22 AM in response to sanfm12

Hello


You may create Automator service to receive text from any application and put the text in current document in specific application.


Here's an example to put the received text in front document of Pages (v4), which will behave almost the same as paste operation except for the CR appended at end of received text (that you may remove if you wish).


User uploaded file



Service definition is as follows:


Input = received text in any application

Code for Run AppleScript action =


on run {input, parameters}
    tell application "Pages"
        if not (exists document 1) then make new document
        tell document 1
            set selection to "" & input & return
            select insertion point after text -1
        end tell
    end tell
end run


As you see, Service facility only takes care of the input part and you need to implement your own ouput part for each application you want to use. Some application provides its own services off-the-shelf, such as to make new note from selection etc. Check Services Preferencs in System Preferences.


* You may invoke this serivce by first selecting text in source application, and then select [Application] > Services > [this service name]. You may invoke it by using keyboard shortcut if you defined it.


Code is tested with Pages v4.0.5 under 10.6.8.


Regards,

H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to copy in app A and paste in app B without leaving app A?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.