Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a variable in AppleScript (called from within Automator)

Instead of "asdf", I want to use the input variable here.

Seems like this should be easy, but for some reason I can't figure it out.

on run {input, parameters}
-- Type 'asdf'
delay 1.900231
set timeoutSeconds to 2.0
set uiScript to "keystroke \"asdf\""
my doWithTimeout(uiScript, timeoutSeconds)
return input
end run

on doWithTimeout(uiScript, timeoutSeconds)
set endDate to (current date) + timeoutSeconds
repeat
    try
        run script "tell application \"System Events\"
" & uiScript & "
end tell"
    exit repeat
    on error errorMessage
        if ((current date) > endDate) then
            error "Can not " & uiScript
        end if
    end try
end repeat
end doWithTimeout

For example, if I try set uiScript to "keystroke input", I get the error from Automator:

The action “Run AppleScript” encountered an error.
Check the actionʼs properties and try running the workflow again.
like image 464
pk1557 Avatar asked Mar 30 '26 03:03

pk1557


1 Answers

Figured it out myself. I feel dumb now.

set uiScript to "keystroke \"" & input & "\""

like image 96
pk1557 Avatar answered Apr 02 '26 15:04

pk1557



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!