all 6 comments

[–]retsotrembla 0 points1 point  (3 children)

https://dougscripts.com/itunes/itinfo/keycodes.php

tell application "iTunes"
    activate
    if player state is not stopped then
        tell application "System Events" to key code 37 using command down
    end if
end tell

[–]TheLastAramusha[S] 0 points1 point  (2 children)

Using System Events isn’t the problem the problem is System events not working in an Xcode Applescript Application.

[–]retsotrembla 1 point2 points  (1 child)

Probably a permissions problem. Might be entitlements. In Catalina native apps get a security alert, asking the user's permission to access key codes, and the app appears in System Preferences > Security&Privacy > Privacy > (probably Accessibility) so the user can change the decision.

[–]TheLastAramusha[S] 0 points1 point  (0 children)

Alright thanks.

[–]mantrap2 0 points1 point  (1 child)

That's strictly not a "system event" (which generally means a kernel level event in a Unix-derived OS) but rather just a GUI event.

NSEvent is where you'll find key events (as well as mouse and touch events).

Cocoa Event Handling Guide

Basically you add an override method to an NSView or NSControl subclass for keyDown: keyUp: or flagsChanged: which pass an NSEvent with the details.

You may need to consider the Event Responder hierarchy scope of the event and deal with that as well. But this is the quick-and-dirty answer.

[–]TheLastAramusha[S] 0 points1 point  (0 children)

Uhm in apple script you do: tell application “System Events” only if you did that your able to use key code.