Applescript to "start Dictation" and "stop Dictation" depending on menu - macos

I have on Applescript to start and another one to stop Dictation. I tried to run them together and it only starts Dictation but does not stop it. I tried the key code way but this script is to be used with VoiceOver and that method does not work. Is there a way to have this script check if start/stop is ready and to act accordingly?
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu bar 1
tell menu bar item "Edit"
tell menu 1
click menu item "Start Dictation"
end tell
end tell
end tell
end tell
end tell
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu bar 1
tell menu bar item "Edit"
tell menu 1
click menu item "Stop Dictation"
end tell
end tell
end tell
end tell
end tell

You can use the exists command
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu 1 of menu bar item "Edit" of menu bar 1
if exists of menu item "Start Dictation" then
click menu item "Start Dictation"
else if exists of menu item "Stop Dictation" then
click menu item "Stop Dictation"
end if
end tell
end tell
end tell
--
Or you can use a word in the name of the menu, like this:
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu 1 of menu bar item "Edit" of menu bar 1
click (first menu item whose name contains " Dictation") -- start or stop
end tell
end tell
end tell

Related

Setting a Popup Buttons Value from Applescript

I'm trying to set an external applications settings with an applescript. I currently have
tell application "System Events"
tell process "Pro Tools"
click menu item "Save Copy In..." of menu "File" of menu bar 1
tell pop up button 4 of window "Save Copy In..."
click
set value to "AIFF"
end tell
end tell
end tell
But nothing happens at the set value point. Any help would be gratefully received! Attached is a screenshot of where I'm stuck at.
I think you just need to wait until the "Save Copy In..." window appears. Try this code, which adds a delay loop looking for the window.
tell application "System Events"
tell process "Pro Tools"
tell menu bar 1's menu "File"
click menu item "Save Copy In..."
end tell
repeat until (exists window "Save Copy In...")
delay 0.1
end repeat
tell window "Save Copy In..."'s pop up button 4
click
-- the menu does not appear inthe PUB's view heirarchy until after it's clicked
tell menu 1
click menu item "AIFF" -- 'pick' should work too
end tell
end tell
end tell
end tell
Try to insert a short delay to make sure the menu is available and rather than setting the value pick the menu item.
tell application "System Events"
tell process "Pro Tools"
click menu item "Save Copy In..." of menu "File" of menu bar 1
tell pop up button 4 of window "Save Copy In..."
click
delay 0.2
pick menu item "AIFF" of menu 1
end tell
end tell
end tell
The command pick is not documented, it's a synonym of click.

How to take user voice input in applescript

I want to be able to take in input from the user in a program and have them speak it instead of type it into a text box. Is there any way to do this?
Any help would be appreciated.....
If you change the shortcut used to start dictation (System Preferences > Keyboard > Dictation > Shortcut) to either Press Left Command Key Twice or Press Either Command Key Twice, you can trigger this shortcut using System Events (provided the necessary accessibility privileges are granted):
delay 0.1
tell application "System Events" to repeat 2 times
key down command
key up command
end repeat
Alternatively, you can trigger the menu item:
tell application "System Events" to tell ¬
(the first process whose frontmost is true) to tell ¬
menu bar 1 to tell ¬
menu bar item "Edit" to tell ¬
menu "Edit" to tell ¬
menu item "Start Dictation" to ¬
if exists then click it
Edited to correct the referencing of the menu item chain, as highlighted by #wch1zpink, which previously prevented it working in *Google Chrome*.
#CJK really deserves the vote love, as this is only an adjusted extension of his thorough answer...
As taken from #CJK 's solution...
"Alternatively, you can trigger the menu item:"
tell application "System Events" to tell ¬
(the first process whose frontmost is true) to tell ¬
menu bar 1 to tell ¬
menu "Edit" to tell ¬
menu item "Start Dictation" to ¬
if exists then click it
Worked in almtost every application I tested the above code with... It did not work in Google Chrome.
However, this minor adjustment to his original code, Will work in Google Chrome.
tell application "System Events" to tell (the first process whose frontmost is true)
tell menu bar 1 to tell menu bar item "Edit"
tell menu "Edit"
try
click menu item "Start Dictation"
end try
end tell
end tell
end tell
For either of these solutions to actually "Enable Dictation" the mouse cursor must be in a field which allows text input, before the code is run.

Applescript to input text to find bar in browser

I can start a "Find" operation with the following applescript:
activate application "Firefox"
tell application "System Events"
tell process "Firefox"
click menu item "Find" of menu 1 of menu bar item "Edit" of menu bar 1
end tell
end tell
How can I then input a string to the find search?
activate application "Firefox"
tell application "System Events"
tell process "Firefox"
click menu item "Find" of menu 1 of menu bar item "Edit" of menu bar 1
end tell
keystroke myString
keystroke return
end tell

How to set iTunes 11 in shuffle or repeat mode via applescript

According to http://dougscripts.com/ setting shuffle and repeat modes via applescript is broken in iTunes 11.
According to this stackoverflow answer shuffle is now a playlist independent setting.
Thus, I tried to set the shuffle value via the UI, either by the LCD-ish display of iTunes or via the menu bar. All I could get was "unknown UI index" errors when trying to click the shuffle button/menu item, either in the LCD area or the menu bar. (I'm new to applescript).
If some of you could come up with a way to toggle shuffle mode on iTunes 11, that would be great. Also I'd prefer a solution based on the menu bar rather than the LCD display since the shuffle button is not always visible in the latter.
Ideally, I'd prefer a semantic-based solution over a UI-based solution but I'm not sure if it's possible (iTunes 11 applescript library seems to be outdate since it mention a "shuffle" property for "playlists" items).
For the new Music app, this works. If you're using iTunes still, change "Music" to "iTunes".
Repeat can be set to one or off or all.
tell application "Music"
set song repeat to off
end
Shuffle can be set to true or false.
tell application "Music"
set shuffle enabled to true
end
You can find more details at Dougscripts.
I liked John Sauer's approach so much I wrote myself some getters/setters for these properties using his approach. It's works well because you do not have to activate iTunes before using them. Anyway, I thought I'd post them in case they're of help to anyone. You will get or set their values using the "types" (modeled after the menu item names) as follows:
Repeat types are "Off", "All", or "One".
Shuffle types are "Off", "By Songs", "By Albums", or "By Groupings"
on getRepeatType() -- the return value is a string: Off/All/One
tell application "System Events"
tell process "iTunes"
set menuItems to menu items of menu bar 1's menu bar item "Controls"'s menu 1's menu item "Repeat"'s menu 1
set currentChoice to "unknown"
repeat with anItem in menuItems
try
set theResult to value of attribute "AXMenuItemMarkChar" of anItem
if theResult is not "" then
set currentChoice to name of anItem
exit repeat
end if
end try
end repeat
end tell
end tell
return currentChoice
end getRepeatType
on setRepeatType(repeatType) -- repeatType is a string: Off/All/One
set currentValue to my getRepeatType()
ignoring case
if currentValue is not repeatType then
tell application "System Events" to tell process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Repeat"'s menu 1
if repeatType is "all" then
perform action "AXPress" of menu item "All"
else if repeatType is "one" then
perform action "AXPress" of menu item "One"
else
perform action "AXPress" of menu item "Off"
end if
end tell
end if
end ignoring
end setRepeatType
on getShuffleType() -- the return value is a string: Off/By Songs/By Albums/By Groupings
tell application "System Events"
tell process "iTunes"
set menuItems to menu items of menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s menu 1
set onOffItemName to name of item 1 of menuItems
end tell
end tell
-- is shuffle off
ignoring case
if onOffItemName contains " on " then return "Off"
end ignoring
-- shuffle is on so find how we are shuffling
set currentChoice to "Unknown"
tell application "System Events"
tell process "iTunes"
repeat with i from 2 to count of menuItems
set anItem to item i of menuItems
try
set theResult to value of attribute "AXMenuItemMarkChar" of anItem
if theResult is not "" then
set currentChoice to name of anItem
exit repeat
end if
end try
end repeat
end tell
end tell
return currentChoice
end getShuffleType
on setShuffleType(shuffleType) -- shuffleType is a string: Off/By Songs/By Albums/By Groupings
set currentValue to my getShuffleType()
script subs
on toggleShuffleOnOff()
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s menu 1 whose name ends with "Shuffle")
end toggleShuffleOnOff
on pressBySongs()
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s menu 1 whose name ends with "Songs")
end pressBySongs
on pressByAlbums()
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s menu 1 whose name ends with "Albums")
end pressByAlbums
on pressByGroupings()
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s menu 1 whose name ends with "Groupings")
end pressByGroupings
end script
ignoring case
if shuffleType contains "off" then -- we have to make sure it's off
if currentValue does not contain "off" then subs's toggleShuffleOnOff()
else
-- make sure it's on
if currentValue contains "off" then subs's toggleShuffleOnOff()
-- select the shuffle menu item for the type
if shuffleType contains "song" and currentValue does not contain "song" then
subs's pressBySongs()
else if shuffleType contains "album" and currentValue does not contain "album" then
subs's pressByAlbums()
else if shuffleType contains "group" and currentValue does not contain "group" then
subs's pressByGroupings()
end if
end if
end ignoring
end setShuffleType
I was optimistic when I saw the AppleScript property current playlist of the iTunes application, but it doesn't work well. It's able to get and set the current playlist's name, but it can do neither for the properties shuffle or song repeat. It errors when trying to set either property, and it always returns 'false' for shuffle and 'off' for song repeat.
I think your only option is UI Scripting. Here's how to toggle shuffle through the menu bar:
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s menu 1 whose name ends with "Shuffle")
And here's how to set repeat:
tell application "System Events" to tell process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Repeat"'s menu 1
perform action "AXPress" of menu item "Off"
perform action "AXPress" of menu item "All"
perform action "AXPress" of menu item "One"
end tell
For iTunes 12, this works
tell application "System Events"
tell process "iTunes" to if exists then
click menu item "Albums" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar 1
end if
end tell
Change Albums to Songs, Groupings, On and Off accordingly.
Here is another approach:
activate application "iTunes"
tell application "System Events"
tell process "iTunes"
click menu item 1 of menu 1 of menu item "Shuffle" of menu 1 of menu bar item "Controls" of menu bar 1
end tell
end tell
It looks like many of these scripts are broken in the newest iTunes. Here are two the work:
tell application "System Events" to tell UI element "iTunes" of list 1 of process "Dock"
if not (exists) then return
perform action "AXShowMenu"
click menu item "Shuffle" of menu 1
end tell
That one toggles shuffle via the Dock. You can watch the Dock menu animate when you use it.
This one toggles shuffle via the menu, invisibly:
tell application "System Events"
tell application process "iTunes"
tell menu 1 of menu item "Shuffle" of menu "Controls" of menu bar 1
if (value of attribute "AXMenuItemMarkChar" of item 1 of menu items as string) = "✓" then
click menu item 2
else
click menu item 1
end if
end tell
end tell
end tell
Both of these will work even with iTunes is in the background.
Spent some time deconstructing all the obfuscated solutions in this post (which appear to not work anymore), here's a more readable and customizable approach that works with iTunes 12.1:
tell application "System Events"
set itunesMenuBar to process "iTunes"'s first menu bar
set controlsMenu to itunesMenuBar's menu bar item "Controls"'s first menu
set shuffleMenu to controlsMenu's menu item "Shuffle"'s first menu
set shuffleOnMenuItem to shuffleMenu's menu item "On"
set shuffleSongsMenuItem to shuffleMenu's menu item "Songs"
tell process "iTunes"
click shuffleOnMenuItem
click shuffleSongsMenuItem
end tell
end tell
This will turn shuffle on and set it to shuffle songs instead of albums, and it should be pretty obvious how to change it to do other things.

Applescript open all file in folder one by one

I'm only a beginner sorry if my question look stupid.
Right now I'm trying to write a script that convert all my .webarchive to .html, so I end up using application called "iCab" to open .webarchive and save it to .html
Here is the question I've all my .webarchive in save folder and I need a loop to open it all one by one, so I can put this fragment of code inside the loop
tell application "iCab" to open file file_name
tell application "iCab" to activate
delay 1
tell application "System Events"
tell process "iCab"
click menu item "Save As…" of menu "File" of menu bar 1
key code 76
click menu item "Close Window" of menu "File" of menu bar 1
end tell
end tell
Thank you in advance
You also need to convert the files from Finder file objects to aliases.
tell application "Finder"
set fl to files of folder POSIX file "/Users/username/Folder/" as alias list
end tell
repeat with f in fl
tell application "iCab"
open f
activate
end tell
tell application "System Events"
tell process "iCab"
click menu item "Save As…" of menu "File" of menu bar 1
keystroke return
click menu item "Close Window" of menu "File" of menu bar 1
end tell
end tell
end repeat
Try:
tell application "Finder" to set myFiles to every file of folder "Mac OS X:Users:Angeloid:Desktop:yourFolder"
repeat with aFile in myFiles
--insert your code here
end repeat
EDIT to include choose folder:
tell application "Finder" to set myFiles to every file of (choose folder)
repeat with aFile in myFiles
tell application "iCab"
open file file_name
activate
end tell
delay 1
tell application "System Events"
tell process "iCab"
click menu item "Save As…" of menu "File" of menu bar 1
key code 76
click menu item "Close Window" of menu "File" of menu bar 1
end tell
end tell
end repeat

Resources