I am using applescript in midipipe application to run the scripts below, so that my novation launchpad can be mapped to hotkeys in OBS:
Code is:
on runme(message)
#Pad 1
#11,11, CTRLZ
if (item 2 of message = 11)
tell application "System Events"
keystroke "z" using control down
end tell
end if
#Pad 2
#12,12, CTRLX
if (item 2 of message = 12)
tell application "System Events"
keystroke "x" using control down
end tell
end if
#Pad 3
#13,13, CTRLY
if (item 2 of message = 13)
tell application "System Events"
keystroke "y" using control down
end tell
end if
#Pad 4
#14,14, CTRLE
if (item 2 of message = 14)
tell application "System Events"
keystroke "e" using control down
end tell
end if
end runme
BUT
as soon as i had another pad it "contains errors" when compiling, i'm unable to see what the error actually is and the code for pad 5 is the same as the rest of the other parts.
on runme(message)
#Pad 1
#11,11, CTRLZ
if (item 2 of message = 11)
tell application "System Events"
keystroke "z" using control down
end tell
end if
#Pad 2
#12,12, CTRLX
if (item 2 of message = 12)
tell application "System Events"
keystroke "x" using control down
end tell
end if
#Pad 3
#13,13, CTRLY
if (item 2 of message = 13)
tell application "System Events"
keystroke "y" using control down
end tell
end if
#Pad 4
#14,14, CTRLE
if (item 2 of message = 14)
tell application "System Events"
keystroke "e" using control down
end tell
end if
#Pad 5
#19.19, CTRLS
if (item 2 of message = 19)
tell application "System Events"
keystroke “s" using control down
end tell
end if
end runme
Any ideas?
after on runme(message) append:
tell application "OBS"
activate
end tell
Related
Hoping someone can help with this – I'm running macOS Monterey on an Apple M1 Pro laptop. My AppleScript to send out batch text messages, stored on an Excel file, delivered through the Messages app is now not working – it worked fine on my old laptop operating under Catalina.
The problem appears to be in delivering the phone number ("targetBuddyPhone") into the proper location in the Messages app. Instead, the text message ("targetMessage") is being dropped into the recipient location in the app. Does anyone have any ideas on possible solutions?
Thanks in advance.
on run {input, parameters}
set phoneCol to "B"
set messageCol to "C"
set startRow to 1
set counter to "D"
set xlsFilePath to (path to desktop as text) & "texttest.xlsx"
tell application "Microsoft Excel" to open file xlsFilePath
tell application "Microsoft Excel"
set endRow to value of cell (counter & startRow) as number
end tell
repeat with thisRow from startRow to endRow
tell application "Microsoft Excel"
set targetBuddyPhone to string value of cell (phoneCol & thisRow) as string
set targetMessage to value of cell (messageCol & thisRow) as string
end tell
activate application "Messages"
tell application "System Events" to tell process "Messages"
key code 45 using command down -- press Command + N to start a new window
keystroke targetBuddyPhone -- input the phone number
key code 36 -- press Enter to focus on the message area
delay 3
keystroke targetMessage -- type some message
key code 36 -- press Enter to send
end tell
delay 6
end repeat
return input
end run
Since GUI scripting is always tightly tied to the version of the application, I recommend getting rid of it once and for all and using the following more durable solution:
on run {input, parameters}
set phoneCol to "B"
set messageCol to "C"
set startRow to 1
set counter to "D"
set xlsFilePath to (path to desktop as text) & "texttest.xlsx"
tell application "Microsoft Excel"
open file xlsFilePath
set endRow to value of cell (counter & startRow) as number
end tell
tell application "Messages"
activate
set SMSService to service named "SMS" -- YOU NEED THIS SERVICE
end tell
repeat with thisRow from startRow to endRow
tell application "Microsoft Excel"
set targetBuddyPhone to string value of cell (phoneCol & thisRow) as string
set targetMessage to value of cell (messageCol & thisRow) as string
end tell
tell application "Messages"
set theBuddy to buddy targetBuddyPhone of SMSService
send targetMessage to theBuddy
end tell
end repeat
return input
end run
I figured out the solution. To my GUI-based approach, I inserted delays into the script, which solved the problems.
on run {input, parameters}
set phoneCol to "B"
set messageCol to "C"
set startRow to 1
set counter to "D"
set xlsFilePath to (path to desktop as text) & "texttest.xlsx"
tell application "Microsoft Excel" to open file xlsFilePath
tell application "Microsoft Excel"
set endRow to value of cell (counter & startRow) as number
end tell
repeat with thisRow from startRow to endRow
tell application "Microsoft Excel"
set targetBuddyPhone to string value of cell (phoneCol & thisRow) as string
set targetMessage to value of cell (messageCol & thisRow) as string
end tell
activate application "Messages"
tell application "System Events" to tell process "Messages"
key code 45 using command down -- press Command + N to start a new window
delay 3
keystroke targetBuddyPhone -- input the phone number
delay 3
key code 36
delay 3
key code 36 -- press Enter to focus on the message area
delay 3
keystroke targetMessage -- type some message
delay 3
key code 36 -- press Enter to send
end tell
delay 6
end repeat
return input
end run
To Robert's solution, I changed one line (set SMSService to ...) and this script now works properly.
on run {input, parameters}
set phoneCol to "B"
set messageCol to "C"
set startRow to 1
set counter to "D"
set xlsFilePath to (path to desktop as text) & "texttest.xlsx"
tell application "Microsoft Excel"
open file xlsFilePath
set endRow to value of cell (counter & startRow) as number
end tell
tell application "Messages"
activate
set SMSService to 1st account whose service type = SMS -- YOU NEED THIS SERVICE
end tell
repeat with thisRow from startRow to endRow
tell application "Microsoft Excel"
set targetBuddyPhone to string value of cell (phoneCol & thisRow) as string
set targetMessage to value of cell (messageCol & thisRow) as string
end tell
tell application "Messages"
set theBuddy to participant targetBuddyPhone of SMSService
send targetMessage to theBuddy
end tell
end repeat
return input
end run
I'm a beginner working on a script that counts windows in locations on screen. So far it works fairly well, although one flaw is it counts windows that are minimized.
Here is the working code currently:
tell application "System Events"
set winCount1 to 0
set winCount2 to 0
set winCount3 to 0
set winCount4 to 0
set theProcesses to application processes
repeat with theProcess from 1 to count theProcesses
if visible of process theProcess is true then
tell process theProcess
repeat with x from 1 to (count windows)
if ((description of window x is not "dialog") then
set Pos to position of window x
if item 1 of Pos is less than -960 then
set winCount1 to winCount1 + 1
else if item 1 of Pos is less than 0 then
set winCount2 to winCount2 + 1
else if item 1 of Pos is less than 960 then
set winCount3 to winCount3 + 1
else
set winCount4 to winCount4 + 1
end if
end if
end repeat
end tell
end if
end repeat
set countList to {winCount1, winCount2, winCount3, winCount4}
return countList
end tell
Now to try to solve this issue, I tried adding a new condition:
if ((description of window x) is not "dialog") and window x is not miniaturized then
But this returns the error stated in the title. So I tried:
set props to get properties of window x
if props contains miniaturized then
This returns the same error.
I also tried:
set props to get properties of class of window x
if props contains miniaturized then
Same error.
It can't be that difficult to avoid windows without the miniaturized property before testing for it, but I'm not having luck finding a solution. Any ideas?
Get the value of attribute "AXMinimized" of the window, like this:
set winCount1 to 0
set winCount2 to 0
set winCount3 to 0
set winCount4 to 0
tell application "System Events"
repeat with theProcess in (application processes whose visible is true)
tell theProcess
repeat with thisWin in windows
if (description of thisWin is not "dialog") and not (value of attribute "AXMinimized" of thisWin) then
set Pos to position of thisWin
if item 1 of Pos is less than -960 then
set winCount1 to winCount1 + 1
else if item 1 of Pos is less than 0 then
set winCount2 to winCount2 + 1
else if item 1 of Pos is less than 960 then
set winCount3 to winCount3 + 1
else
set winCount4 to winCount4 + 1
end if
end if
end repeat
end tell
end repeat
end tell
return {winCount1, winCount2, winCount3, winCount4}
So for an infinite monkey theorem test i need applescript to input random numbers (0-6) into TextEdit.
I have a working code which doesn't slow down the cpu or anything, but i can't terminate it after running it once without terminating TextEdit.
So my question is:
How can i improve this code so it allows me to terminate it without any concerns.
delay 2
repeat
delay 0.2
tell application "System Events"
if (item 1 of (get name of processes whose frontmost is true)) is "TextEdit" then
set r to (random number from 0 to 1)
if (r = 0) then
keystroke "0"
else if (r = 1) then
keystroke "1"
end if
end if
end tell
end repeat
It will take a long time to create a large sample with a .2 delay and keystrokes.
Try something like this:
set csvOutputPath to (POSIX path of (path to desktop as text)) & "test.txt"
set fileRef to (open for access csvOutputPath)
close access csvOutputPath
set numberList to {}
set counter to 0
repeat
set end of numberList to (random number from 0 to 1)
set counter to counter + 1
if (counter mod 10000) = 0 then
write (numberList as text) to csvOutputPath starting at eof
set numberList to {}
end if
end repeat
Since OSX seems to lack a keyboard shortcut to create a new space/desktop, I'm wondering if this could be accomplished creating an applescript and binding that to a keyboard shortcut.
Looked a ton on google and couldn't find anything. Surprised no one has asked this before... seems like it would be something that lots of people would find very useful.
Here are the list of AppleScript Spaces routines I found online ::
--my get_spaces_enabled()
--my set_spaces_enabled(enable_spaces)
--my toggle_spaces_enabled()
--my get_spaces_rows()
--my set_spaces_rows(row_count)
--my get_spaces_columns()
--my set_spaces_columns(column_count)
--my get_spaces_count()
--my show_all_spaces()
--my get_spaces_application_bindings()
--my set_spaces_application_bindings(new_bindings)
--my collect_application_in_current_space(application_bundle_id)
--my set_spaces_application_binding_for_application(application_bundle_id, chosen_space)
--my get_space_binding_for_application(application_bundle_id)
--my choose_space_for_current_application()
--my choose_space_for_application(application_bundle_id)
--my remove_spaces_application_binding(application_bundle_id)
--my get_spaces_arrow_key_modifiers()
--my get_spaces_numbers_key_modifiers()
--my switch_to_space(space_number)
--my open_spaces_preferences()
--my display_spaces_not_enabled_error()
on get_spaces_enabled()
tell application "System Events" to tell spaces preferences of expose preferences to return (get spaces enabled)
end get_spaces_enabled
on set_spaces_enabled(enable_spaces)
tell application "System Events" to tell spaces preferences of expose preferences to set spaces enabled to enable_spaces
end set_spaces_enabled
on toggle_spaces_enabled()
my set_spaces_enabled(not (my get_spaces_enabled()))
end toggle_spaces_enabled
on get_spaces_rows()
tell application "System Events" to tell spaces preferences of expose preferences to return (get spaces rows)
end get_spaces_rows
on set_spaces_rows(row_count)
tell application "System Events" to tell spaces preferences of expose preferences to set spaces rows to row_count
end set_spaces_rows
on get_spaces_columns()
tell application "System Events" to tell spaces preferences of expose preferences to return (get spaces columns)
end get_spaces_columns
on set_spaces_columns(column_count)
tell application "System Events" to tell spaces preferences of expose preferences to set spaces columns to column_count
end set_spaces_columns
on get_spaces_count()
return ((my get_spaces_rows()) * (my get_spaces_columns()))
end get_spaces_count
on show_all_spaces()
try
tell application "Finder" to set spaces_app_path to (application file id "com.apple.spaceslauncher") as string
do shell script "open " & quoted form of POSIX path of spaces_app_path
end try
end show_all_spaces
on get_spaces_application_bindings()
tell application "System Events" to tell spaces preferences of expose preferences to return (get application bindings)
end get_spaces_application_bindings
on set_spaces_application_bindings(new_bindings)
tell application "System Events" to tell spaces preferences of expose preferences to set application bindings to new_bindings
end set_spaces_application_bindings
on collect_application_in_current_space(application_bundle_id)
set application_bundle_id to my make_lowercase(application_bundle_id)
set app_bindings to my get_spaces_application_bindings()
my set_spaces_application_bindings((run script "{|" & application_bundle_id & "|:65544}") & app_bindings)
my set_spaces_application_bindings(app_bindings)
end collect_application_in_current_space
on set_spaces_application_binding_for_application(application_bundle_id, chosen_space)
set application_bundle_id to my make_lowercase(application_bundle_id)
if chosen_space is in {0, "None"} then
my remove_spaces_application_binding(application_bundle_id)
else
if chosen_space = "All" then set chosen_space to 65544
my set_spaces_application_bindings((run script "{|" & application_bundle_id & "|: " & chosen_space & "}") & (my get_spaces_application_bindings()))
end if
end set_spaces_application_binding_for_application
on get_space_binding_for_application(application_bundle_id)
set application_bundle_id to my make_lowercase(application_bundle_id)
set app_bindings to my get_spaces_application_bindings()
try
get app_bindings as string
on error error_string
set app_bindings to my string_to_list(text 13 thru -20 of error_string, ", ")
end try
repeat with i from 1 to (count app_bindings)
if item i of app_bindings starts with ("|" & application_bundle_id & "|:") then return (item 2 of (my string_to_list(item i of app_bindings, ":"))) as number
end repeat
return 0
end get_space_binding_for_application
on choose_space_for_current_application()
return my choose_space_for_application(bundle identifier of (info for (path to frontmost application)))
end choose_space_for_current_application
on choose_space_for_application(application_bundle_id)
set application_bundle_id to my make_lowercase(application_bundle_id)
if (not my get_spaces_enabled()) then if (not my display_spaces_not_enabled_error()) then return false
try
tell application "Finder" to set app_path to (application file id application_bundle_id) as string
set app_name to short name of (info for (app_path as alias))
set the_choices to {"None", "All"}
repeat with i from 1 to (my get_spaces_count())
set end of the_choices to i
end repeat
set default_space to my get_space_binding_for_application(application_bundle_id)
if default_space = 0 then
set default_space to "None"
else if default_space = 65544 then
set default_space to "All"
end if
set chosen_space to (choose from list the_choices default items {default_space} with title "Spaces Assigner" with prompt "Assign " & app_name & " to Space:" OK button name "Assign" without multiple selections allowed and empty selection allowed)
if chosen_space = false then return false
my set_spaces_application_binding_for_application(application_bundle_id, item 1 of chosen_space)
return true
on error e number n
log "choose_space_for_application (" & application_bundle_id & ") Error (" & n & "): " & e
return false
end try
end choose_space_for_application
on remove_spaces_application_binding(application_bundle_id)
set application_bundle_id to my make_lowercase(application_bundle_id)
set app_bindings to my get_spaces_application_bindings()
try
get app_bindings as string
on error error_string
set app_bindings to my string_to_list(text 13 thru -20 of error_string, ", ")
end try
set new_bindings to {}
repeat with i in app_bindings
set i to contents of i
if i does not start with "|" & application_bundle_id & "|:" then set end of new_bindings to i
end repeat
my set_spaces_application_bindings(run script "{" & (my list_to_string(new_bindings, ", ")) & "}")
end remove_spaces_application_binding
on get_spaces_arrow_key_modifiers()
tell application "System Events" to tell spaces preferences of expose preferences to return (get key modifiers of (get properties of arrow key modifiers))
end get_spaces_arrow_key_modifiers
on get_spaces_numbers_key_modifiers()
tell application "System Events" to tell spaces preferences of expose preferences to return (get key modifiers of (get properties of numbers key modifiers))
end get_spaces_numbers_key_modifiers
on switch_to_space(space_number)
if not my gui_scripting_check() then return
set key_modifiers to my get_spaces_numbers_key_modifiers()
tell application "System Events"
set key_modifier_list to {}
if key_modifiers contains command then set end of key_modifier_list to "command down"
if key_modifiers contains control then set end of key_modifier_list to "control down"
if key_modifiers contains option then set end of key_modifier_list to "option down"
if key_modifiers contains shift then set end of key_modifier_list to "shift down"
set key_modifier_list to my list_to_string(key_modifier_list, ", ")
if key_modifier_list ≠ "" then set key_modifier_list to " using {" & key_modifier_list & "}"
end tell
run script ("tell application \"System Events\" to keystroke \"" & space_number & "\"" & key_modifier_list)
end switch_to_space
on open_spaces_preferences()
tell application "System Preferences"
activate
tell pane id "com.apple.preference.expose" to reveal anchor "Spaces"
end tell
end open_spaces_preferences
on display_spaces_not_enabled_error()
beep
activate
if ((button returned of (display dialog "Spaces is not enabled. Would You like to enable it now?" with title "Spaces Error" buttons {"Keep Disabled", "Enable"} default button 2 with icon 0)) = "Keep Disabled") then return false
my set_spaces_enabled(true)
return true
end display_spaces_not_enabled_error
on list_to_string(l, d)
tell (a reference to my text item delimiters)
set {o, contents} to {contents, d}
set {l, contents} to {"" & l, o}
end tell
return l as Unicode text
end list_to_string
on string_to_list(s, d)
tell (a reference to my text item delimiters)
set {o, contents} to {contents, d}
set {s, contents} to {s's text items, o}
end tell
return s
end string_to_list
on gui_scripting_check()
tell application "System Events" to set gui_scripting_enabled to UI elements enabled
if not gui_scripting_enabled then
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
display dialog "This application utilizes the built-in Graphic User Interface Scripting architecture of Mac OS X which is currently disabled." & return & return & "You can activate GUI Scripting by selecting the checkbox \"Enable access for assistive devices\" in the Universal Access preference pane." with icon 2 buttons {"OK"} default button 1
end tell
end if
return gui_scripting_enabled
end gui_scripting_check
on make_lowercase(the_string)
return do shell script "echo " & quoted form of the_string & " | /usr/bin/perl -pe 'use encoding utf8; s/(\\w)/\\L$1/gi'"
end make_lowercase
You can use these to make your code :)
Link to the routines site :: MacScripter - Applescript Spaces Routine
Having trouble figuring out this loop - I want the below function to repeat ONLY if myText changes (i.e. the album the individual is listening to changes) - I have been able to make it loop a certain number of times or repeat in the event the track changes, but I want it to repeat in the event the album changes.
tell application "iTunes"
repeat if myText changes
if player state is playing then
set albumName to (get album of current track)
set myText to text 1 thru 10 of albumName
end if
end repeat
end tell
open location "http://bandsite.com/shows/" & myText
When the code works without the repeat command it looks like this:
tell application "iTunes"
if player state is playing then
set albumName to (get album of current track)
set myText to text 1 thru 10 of albumName
end if
end tell
open location "http://bandsite.com/shows/" & myText
I need the whole function to repeat in the event myText changes
Save this as a stay open application:
property myList : {}
on run
set albumA to my playCheck()
if albumA ≠ false then
set end of myList to albumA
else
quit -- Quit on the next idle.
return 1 -- Please send the next idle as soon as possible.
end if
end run
on idle
set albumA to my playCheck()
if albumA ≠ false then
set end of myList to albumA
if (item -1 of myList) ≠ (item -2 of myList) then
open location "http://bandsite.com/shows/" & (text 1 thru 10 of albumA)
end if
end if
return 3
end idle
on playCheck()
tell application "iTunes"
if player state is playing then return (get album of current track)
return false
end tell
end playCheck
temptext = mytext
do
if temptext != myText
tell application "iTunes"
if player state is playing then
set albumName to (get album of current track)
set myText to text 1 thru 10 of albumName
end if
end tell
temptext = myText
end if
while temptext==text
Try this
Hope this is what you want...