1

I am looking to automate Copy, Paste, Tab and Left in Microsoft Excel and either Safari or Firefox using Applescript. I have limited experience in coding, and have recently complied a code, but unfortunately it doesn't work as planned...

What I want to happen;

  1. In Browser Copy
  2. In Excel Paste, Left, Copy
  3. In Browser Tab, Paste, Tab x8
  4. Repeat
repeat 50 times

    tell application "Safari" to activate
    tell application "System Events"
        tell process "Safari"
            keystroke "c" using command down
        end tell
    end tell

    delay 0.3

    tell application "Microsoft Excel" to activate
    tell application "System Events"
        tell process "Microsoft Excel"
            keystroke "v" using command down
            delay 0.3
            key code 123
            keystroke "c" using command down
        end tell
    end tell

    tell application "Safari" to activate
    tell application "System Events"
        tell process "Safari"
            keystroke tab
            keystroke "v" using command down
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
            keystroke tab
        end tell
    end tell

    delay 0.3

end repeat
1

Its hard to know exactly what you are trying to accomplish but some improvements were introduced into the code below, with comments added in the code. Several points:

  1. Note that depending on how fast/slow your system is responding you may need small delays after every keystroke or action. You'll have to figure the minimum time empirically.

  2. Any time you "activate" or launch Safari, Excel or any application within the script there will be a delay until the app is up and running. To deal with this, you will need these apps running prior to running the script, or you need to introduce long enough delays into your script, or better yet you need to add code that pauses the script until the app is launched.

  3. When you open Safari and Excel you are not specifying any specific window, file or URL to target for the subsequent keystrokes. So as written the script will default to the front window/tab of each app. I guess that's OK if what you are targeting is the front window. If no open window exists then the script will fail. So for example, to get around this I added code to open a new excel file.

  4. As currently constructed, the script copies from the front window in Safari, then eventually comes back to the very same window to execute the tab - paste - tab x 8. One has to assume that was the intention?

  5. As currently constructed the default is to paste to cell A1 of Excel. Then you tell Excel to move to once cell to the left, but there is no cell to the left of A1.

  6. Much of the code is written for GUI scripting. Where possible its better to use Applescript commands to accomplish the desired activity. I'll just leave it like that for now. You probably have enough on your mind learning the basics right now.

    tell application "Safari" to activate
    
    tell application "System Events"
        tell process "Safari"
        keystroke "c" using command down
        delay 0.3
        end tell
    end tell
    
    
    tell application "Microsoft Excel" to activate
        delay 5 -- time required to launched Excel if not already open
    
    tell application "System Events"
        tell process "Microsoft Excel"
        keystroke "n" using command down -- open new excel page
        delay 0.5 -- wait for new excel page to open
        keystroke "v" using command down
        delay 0.2 -- wait for key stroke entry
        key code 123
        delay 0.2 -- wait for key stroke entry
        keystroke "c" using command down
        delay 0.2 -- wait for key stroke entry
    end tell
    
    end tell
    
    
    tell application "Safari" to activate
    
    tell application "System Events"
        tell process "Safari"
        keystroke tab
        delay 0.2 -- wait for key stroke entry
        keystroke "v" using command down
        delay 0.2 -- wait for key stroke entry
    
        repeat 8 times
            keystroke tab
            delay 0.2 -- wait for key stroke entry
        end repeat
    
    end tell
    
    end tell
    
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .