Wanting to run some Javascript from within AppleScript

Joined
Feb 22, 2025
Messages
2
Reaction score
0
This could see me lots of time if I can get it to work. My current manual effort is to copy an item from an excel spreadsheet, switch to Safari, press a button to (Create your own) which unhides a textbook, I then paste the excel item into the textbook and press the Add button - then go back to Excel to get next item over and over again.

I have some AppleScript code that captures items from an excel spread sheet and each item is passed to the Javascript code to hopefully do the click button and paste functions.

Since it is my own computer, I ty to control the environment by having just excel and just the workbook for may entries open and just one Safari tab open with the page having my buttons and input field on it.

SO far the Excel part seems to be working fine but when I tell Safari o do the Javascript it is not working.

Thank you


The current code is
Code:
on run
    
    tell application "Microsoft Excel"
        activate
        set itemcount to value of cell "A1"
        display dialog itemcount
        
        repeat with i from 2 to itemcount + 1
            activate "Microsoft Excel"
            
            set newcell to "A" & i
            set output to value of cell newcell
            display dialog output
            
            tell application "Safari" to tell document 1 to do JavaScript
            "document.getElementsById('msku-custom-option-link').click();"
            "document.getElementsById('msku-custom-option-input').value=output"
            "document.getElementsById('msku-custom-option-add').click();"
        end repeat
        
    end tell
    
    -- button1 msku-custom-option-link
    -- text msku-custom-option-input
    -- button2 msku-custom-option-add
    
end run
 

Attachments

  • Screen Shot 2025-02-22 at 1.03.10 PM.png
    Screen Shot 2025-02-22 at 1.03.10 PM.png
    24.2 KB · Views: 5
  • Screen Shot 2025-02-22 at 1.03.24 PM.png
    Screen Shot 2025-02-22 at 1.03.24 PM.png
    22.1 KB · Views: 4
Joined
Feb 22, 2025
Messages
2
Reaction score
0
I've modified the code to get closer to what I want - it now pulls each item from the excel file and goes to safari and gets/displays the document.readyState.

I'd really like the Javascript to
a) simulate clicking on button named msku-custom-option-link
b) paste the value of output into a textfield named msku-custom-option-input
c) simulate clicking on button named msku-custom-option-add

My issues -
getting the Javascript code to compile without errors
wondering if the variable output is available to Javascript or do I need to pass it somehow.


Code:
on run
    
    tell application "Microsoft Excel"
        activate
        set itemcount to value of cell "A1"
        display dialog itemcount
        
        repeat with i from 2 to itemcount + 1
            activate "Microsoft Excel"
            
            set newcell to "A" & i
            set output to value of cell newcell
            
            
            
            tell application "Safari"
                
                if (do JavaScript "document.readyState" in document 1) is "complete" then set pageLoaded to true
                display dialog pageLoaded as string
            end tell
            
        end repeat
        
    end tell
end run
`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,378
Messages
2,571,928
Members
48,765
Latest member
AIZSherryl

Latest Threads

Top