Gui scripting using AppleScript not working

Before i get too complicated, I am starting with getting a basic copy/paste using keyboard shortcuts to work. In theory if i can copy a cells value into another cell using keyboard shortcuts, i can use any keyboard shortcuts i need for more complicated scripts.


Can someone more knowledgable than I look over the below core code and tell me what i am doing wrong?

(I have added script editor to accessibilities security settings to allow it to control the computer.)


My code for a simple copy/paste in numbers (other programs also don't respond):

onrun

tellapplication "Numbers"

activate

ifnot (existsdocument 1) thenerrornumber 1000

telldocument 1

tell active sheet

settheselectedTableto (thefirsttablewhoseclassofselection rangeisrange)

setmyRowstocounttherowsinselectedTable

tell selectedTable

settheselection rangetocell 2 ofrow 3

sendCopy

settheselection rangetocell 4 ofrow 3

sendPaste

endtell

endtell

endtell

endtell

endrun

on sendCopy()

tellapplication "Numbers" toactivate

tellapplication "System Events"

tell application process "Numbers"

keystroke "C"

endtell

endtell

end sendCopy

on sendPaste()

tellapplication "Numbers" toactivate

tellapplication "System Events"

tell application process "Numbers"

keystroke "V"

endtell

endtell

end sendPaste


Thanks

Jason

iPad, iOS 9.3.2, iPad, macMini, iPhone

Posted on Jun 19, 2016 11:08 AM

Reply
6 replies

Jun 20, 2016 5:47 AM in response to VikingOSX

1) I cannot locate any reference to the keystroke command in the website you linked. (yes i did spend about half an hour searching)

The format I used {} is found in almost every sample i have found online so far. (three of the dozens linked below)

Sample1 | Sample2 | Sample3


2) I did try without the {} and it still doesn't work

3) I also tried just passing a keypress ( Keystroke "C") after selecting a cell.


4) I read the support docs on this from apple and cannot get the warning to appear. I have no reference to Script Editor in the security dialogs.


This is what makes me think there is something odd with the system setup. It gives no warning that a program is trying to control the computer. Which is supposed to happen.


What system setting might be supressing the "not allowed assistive access" warning I should be seeing?


Thanks

Jason

Jun 20, 2016 6:22 PM in response to jaxjason

Well, my bad. The keystroke reference is within the Processes Suite for the System Events scripting dictionary. You can get there from the Scripting Dictionary File menu.


The keystroke "C" and keystroke "V" commands do not perform copy and paste — as do command+C and command+V — which are written as I posted previously. The use of curly braces after the using clause is enforced when multiple actions are requested (.e.g using {command down, shift down}). Again, these correspond to their menu item keyboard shortcuts.


In your handlers, I see redundancy in telling application numbers to activate (as it is already activated) when you are associating the keystrokes with the application process numbers. Keystrokes always apply to the front most application, and in this case, it is the application process numbers.

Jun 20, 2016 12:39 PM in response to VikingOSX

The keystroke "C" and keystroke "V" commands do not perform copy and paste


Yeah, I know they don't perform copy/paste.


I was seeing if any keystrokes were being accepted by the system and pushed to the topmost program.

By using Keystroke "C" without modifiers, 'system events' should have 'pressed' the C key and put the letter C in the selected cell. This failed.

I also tried just using the Command C command to copy, then try to paste into another cell manually using the Command V combination on my keyboard. Fail.


With both of those failing, I conclude that no keypresses, modified for keyboard shortcuts or just plain letters being typed, are being processed by the system events.


I understand the process on top is supposed to receive the keypresses, but i was getting nothing, so i put those in to ensure ti was the topmost program (one more thing i was trying to verify, Make sure the proper app is still on top, using gui code didnt activate some other app by accident)


Back to the main thing that seems to be missing....


What system setting might prevent the warning mentioned in the support document linked above, from showing. This seems to be the issue. My system is blocking system events from even being processed and trying to control the machine. If it is blocked completely, then it doesn't matter that i put script editor on the security screen.


Thanks

Jason

Jun 20, 2016 6:20 PM in response to VikingOSX

I did already set script editor as allowed in security


but i thinking just figured it out.


I removed the subroutines, and converted them into code within the original script (i.e. not use subroutines for copy/paste. just repeat the code every time i need it, which i hate doing but i wanted to make sure it wasn't some applescript format issue) this got a reaction from numbers finally.


BUT it was bringing up the color dialog every time! which is not the same keyboard shortcut. Its Shift-Command C... wait a second....


turns out we both typed a capital C, which keystroke interpreted as Shift C.


When i turn it a lower case c, move all the code back in the main routine, it finally works.


Here is the final code:

on run

tell application "Numbers"


activate

if not (exists document 1) then error number 1000

tell document 1

tell active sheet

set the selectedTable to (the first table whose class of selection range is range)

tell selectedTable

set the selection range to cell 2 of row 3

tell application "System Events"

tell process "Numbers"


keystroke "c" usingcommand down

end tell

end tell

set the selection range to cell 4 of row 3

tell application "System Events"

tell process "Numbers"


keystroke "v" usingcommand down

end tell

end tell

end tell

end tell

end tell

end tell

end run

thanks for trying so hard for me.

Jason

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.

Gui scripting using AppleScript not working

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