Can I copy and store text in Applescript?

I have a form on a browser with 6 fields

- I start off in the first field, I select all and them copy that field - I want to store in a variable in Applescript

- I tab to the next field and copy and store in another variable

- I tab to next fields and do the same


I have the variables stored in Applescript


I now go to another browser window where I have another form that needs to be filled - with the data from the first


- with the window active, I press tab - this places me in the first field

- I then input the first variable

- I tab to the next fields and do the same

- I tab to a last field, here I have to press the 'home' key


Can all of the above be done using Applescript?


I do this on my pc laptop using Autohotkey


But I really want to use my Apple laptop instead which lays gathering dust until I can figure out how to do the above mentioned


Thanks



Omar

Posted on Nov 21, 2014 7:47 PM

Reply
8 replies

Nov 23, 2014 12:03 PM in response to omar.m

This is doable.


I have a form on a browser with 6 fields

- I start off in the first field, I select all and them copy that field - I want to store in a variable in Applescript

You can read the copy buffer in applecript.


- I tab to the next field and copy and store in another variable

You can "press" the tab key from applescript


I have the variables stored in Applescript.


You can paste from applescript.

Nov 23, 2014 5:28 PM in response to omar.m

yes, that is doable, but the best way to do it is a matter for some discussion. if you are using Safari, Firefox, or any other browser that gives access to javascript you can use applescript or automator (but it entails looking at and reading the page source); you can GUI script it using applescript as well, and there are a couple of third party utilities (I think) that might be brought to bear. So the first question, I suppose, is what browser you are using and whether you have access to the page source.

Nov 23, 2014 6:04 PM in response to twtwtw

oh... i was hoping the answer would be straight forward


i use chrome, safari and firefox


the main requirement is storing variable values - can this not be done in applescript?

i did look up and saw 'global variables' - i assumed these would give me what i needed?

as long as i can copy some text and store in a variable in applescript - that's all i need

since other key presses, i assume i can do in applescript?


i asked a similar question in the alfred forums and the answer i've been given is yes, i can do what i want


the only thing is, i assumed it could all be done in applescript without needing to use a 3rd party tool?


let me know what u think


javascript: can't see how this would be best done. would need storing local variables outside of the browser.

since the 2 browsers need to be logged into 2 sessions (i didn't mention in the beginning to avoid making sound complex)


i thought i'd ask on the forums before i spent half a day finding out if it's possible 🙂

with autohotkey on windows, i have a simple but neat solution to give what i want - i just want to escape windows and work on my macbook


thanks

Nov 23, 2014 6:53 PM in response to omar.m

Storing variables is easy. Getting the data from the correct place in browser and writing it back to a new place is more complex. the computer doesn't 'know' what the correct places are the way you do, and so the problem becomes telling the computer how to accurately find the correct places.


In javascript, you can use the document.getElementByID() command to find specific elements. In order to copy or set their values in applescript (using Safari only), use something like:


tell application "Safari"

set copiedData to do javascript "document.getElementByID('field id').value"

do javascript "document.getElementByID('field id').value = " & copiedData

end


if you want to use GUI scripting then you have to identify the proper elements by their GUI nesting, something like:


tell application "System Events"

tell process "Safari"

tell window 1

set text field 3

set copiedData to its value

end tell

tell text field 5

set its value to copiedData

end tell

end tell

end tell

end tell


Neither is horribly difficult, but they each take some work to figure out the correct locations.

Nov 23, 2014 7:50 PM in response to twtwtw

@twtwtw thanks for the reply - amazingly helpful for me 🙂


>> Storing variables is easy

OK... that really helps to know. I've never used Applescript and have no idea of what can and can't be done.


OK... my bad, maybe I should have given more detail

I start off by placing the cursor into the first field

Then I tell autohotkey to 'select all', then ctrc-c (+ a small pause to make sure clipboard is captured), then assign clipboard to one of its variables, next tab...

Then do the ctrl-c etc until all fields have been captured


For the form filling, I tell AHK to move the mouse to a specific location, then send 'tab' - this makes the first form field become highlighted...

I then paste the first variable, send 'tab', paste second etc


There is one field that is super annoying... the telephone field... the website has it so that you can't do a 'paste'

But, autohotkey is OK... since it doesn't paste - it 'sends' keys one at a time (which happens in less than a blink of an eye)


So, another question for me is if it's possible to 'send keys' instead of 'paste'?


Hope that all makes sense 🙂


Thanks

Nov 23, 2014 9:18 PM in response to rccharles

Here are all the scripting concepts you will need & some references for further info.


AppleScript

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition the book


AppleScript Language Guide pdf download the pdf file


Intro to applescript with sending an email
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-apple script/


Example Applescript code:


(*
Here is the general idea.  Maneuver to the first input field. Assume it is not highlighted. command+a to highlight. command+c to copy to clipboard.  Copy clipboard variable to applescript variable. tab to the next field.

Applescript has properties for saving a variable be runs of a script. You could save your data there. You would have to write two scripts.  One would call a handler in the second script with the property.  I have not tried this.  You could always write the data to a file. 
*)

-- this script creates and inserts an applescript debug statement into an applescript. The variable name comes from the clipboard.

-- get data from clipboard.
set theClip to the clipboard
log "theClip = " & theClip

set statement to "log \"" & theClip & " = \" & " & theClip
log "statement = " & statement


set the clipboard to statement

(*
Here is how to move around a window.

*)

(* 

It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.


    Author: rccharles
    
    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run

 *)


on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    -- You must have the correct web page in safari.
    tell application "Safari"
        activate
    end tell
    delay 1
    
    delay 1
    say "starting input"
    
    
    -- keycode for cursor ...
    -- 123 left
    -- 124 right
    -- 125 down
    -- 126 up
    
    tell application "System Events"
        -- standarize position
        
        -- home key
        key code 115
        -- example file in information for one student
        -- would need to loop through all students.
        keystroke "Robert A Student"
        key code tab
        keystroke "  Grade A"
        
    end tell
    
    say "final position"
    delay 2
    
    
    ---------------------------------------------
    
    How to call one script from another.
    
    I use a property in these script, so check to see
    test it to verify it does what you want.
    --------------  applescript a.app -------------
    
    on run
    set myApp to "/Applications/applescriptFiles/b.app"
    -- some hocus pocus is nesessary to avoid applescript from change path.
    -- set myApp to POSIX file myApp
    
    tell application myApp
        
        launch
        stringSet("stringie set")
    end tell
end run
    
    ------------ applescript b.app ---------------
    
    
    (* 

save this as an application bundle 

invoked by a.app.  

twtwtw write in:
https://discussions.apple.com/thread/4917321?start=15&tstart=0

hocus-pocus: 

Give your script application a unique bundle id string (CFBundleIdentifier) and a unique bundle name (CFBundleName) too, to make life easier.  You set those in the info.plist file.  Launch the script app once to register it with launch services, and system will be able to find it thereafter.

Example:
      <key>CFBundleIdentifier</key>
      <string>com.rccharles.b</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>b</string>
    
    I added the CFBundleIdentifier info above the CFBundleInfoDictionaryVersion line. I left the CFBundleInfoDictionaryVersion and CFBundleName as is.
    
    *)

property myVar : "initial value"

on run
    display dialog myVar
end run

on stringSet(inputVar)
    set myVar to inputVar
    display dialog "myVar is " & myVar
end stringSet

Nov 23, 2014 9:28 PM in response to rccharles

Here is how to create an Applescript Application.


Here is the program:



(*

https://discussions.apple.com/thread/3033221?tstart=30

Required:


Turn on accessability GUI in system preferences.

Optional:
--------
Full Key Codes
http://www.versiontracker.com/dyn/moreinfo/macosx/21215


youpi or iKey
   youpi and iKey are keystroke remapping programs.
   youpi, the free version, works fairly well for me in Mac OS 10.4 although not officially supported.
http://www.versiontracker.com/dyn/moreinfo/macosx/11485&vid=75326
   iKey is the paid version
http://www.scriptsoftware.com/ikey/

*)

on run
    -- Write a message into the event log.
    -- To see, run this applescript in Sript Editor. Click on Event Log tab at bottom of screen.
    -- Click run.
    log "  --- Starting on " & ((current date) as string) & " --- "

    set fromApp to "Safari"
    set toApp to "TextEdit"

    --- We have arrived.


    say "we are beginning now."

    -- Start your application
    tell application fromApp
        activate
    end tell

    delay 1

    say "copy data to clipboard"

    tell application "System Events"
        tell process fromApp


            keystroke "c" using command down
            delay 2

        end tell
    end tell


    -- Start your application
    tell application toApp
        activate
    end tell

    delay 5
    tell application "System Events"
        tell process fromApp
            --  open file menu
            keystroke return
            keystroke return
            keystroke return

            keystroke "v" using command down

        end tell
    end tell

    say "good news, We are done."

end run


The first thing that you need to do is to make the text into an AppleScript program.


Start the AppleScript Editor

/Applications/AppleScript/Script Editor.app

In Snow Leopard it's at: /Applications/Utilities/AppleScript Editor


Copy the script text to the Applescript editor.


Save the text to a file as an application and do not check any of the boxes below.



User uploaded file


If you want access to the script from your Script Menu, move the script (the saved script application file) to your

~/Library/Scripts folder. You can also drag it to your Dock or make an alias for it on the Desktop.<br><br>


To debug, run the script within the Applescript Editor. Click on the event log tab at the bottom of the window. Click on the run icon. The results from the log statement will be shown at the bottom of the screen.



User uploaded file

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.

Can I copy and store text in Applescript?

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