Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript keys pressed

Hay guys,


We are trying to do the following: We want to use applescript to activate a specific track in a specific playlist in iTunes, we wanna do this by using keys pressed or keystroke and then A. We activate the A by linking it back to our Arduino code through keyboard.write('A');. The applescript we're using now is the following:


if (keystroke) = {"A"} then

tell application "iTunes" to play the last track of the first library playlist

else

tell application "iTunes" to play the first track of the first library playlist

end if


(or use keyspressed instead of keystroke), but anyway, it's not working. With the keystroke code it does activate iTunes but thats not what we want eventually, we want that when A is pressed it starts a certain song in iTunes. Please Help

MacBook Pro, OS X Mavericks (10.9.2)

Posted on Apr 7, 2014 5:03 AM

Reply
2 replies

Apr 7, 2014 8:06 AM in response to Ghislaine88

Hello


You may try this. Tested under 10.6.8.


Good luck,

H


(*
    For test, press some key during the delay created by [1].
    Key accompanied with command modifier is ignored as specified by |:mask| option in [2].
    Note that the key pressed will be typed in current document as well.
    To exit, cancel the dialogue.
*)
repeat
    delay 0.2 -- [1]
    set c to key_pressed({|:mask|:"k"}) -- [2]
    if c ≠ "" then
        display dialog c & " (" & c's id & ") is pressed"
    else
        display dialog "none"
    end if
end repeat

on key_pressed(options) -- v0.2
    (*
        record options : control options
            full spec = {|:mask|:m, |:sleep|:s}
            defaults  = {|:mask|:"", |:sleep|:0.1}
            
            string m : modifier flags; e.g. 'ck' = control + command
                a = capslock
                s = shift
                c = control
                o = option
                k = command
            * key pressed accompanied with any modifiers specified in m is ignored
            
            integer i : sleep [sec] put as guard interval before return
    *)
    set defaults to {|:mask|:"", |:sleep|:0.1}
    set {|:mask|:m, |:sleep|:s} to options & defaults
    if m = "" then set m to "%"
    set s to "" & s
    
    set ruby to "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby"
    do shell script ruby & " <<'EOF' - " & m's quoted form & " " & s's quoted form & "
require 'osx/cocoa'
include OSX

m, s = ARGV[0..1]
s = s.to_f
mtable = {
    'a'    => KCGEventFlagMaskAlphaShift,
    's'    => KCGEventFlagMaskShift,
    'c'    => KCGEventFlagMaskControl,
    'o'    => KCGEventFlagMaskAlternate,
    'k'    => KCGEventFlagMaskCommand,
}
mf = m.split(//).inject(0) { |mf, x| (m = mtable[x]) ? mf | m : mf }
sst = KCGEventSourceStateCombinedSessionState
cc = []
unless CGEventSourceFlagsState(sst) & mf != 0        # ignore key accompanied with any modifiers in mf
    (0x00..0x7f).each do |vk| 
        if CGEventSourceKeyState(sst, vk)
            ev = CGEventCreateKeyboardEvent(nil, vk, true)        # vk down
            nsev = NSEvent.eventWithCGEvent(ev)                    # 10.5 or later
            begin
                cc << nsev.characters.to_s
            rescue
            end
        end
    end
end
puts cc.join('')
sleep s if s > 0.0        # optional guard interval
EOF"
end key_pressed

Applescript keys pressed

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