AppleScript to Speak Selected text in Chinese Language - applescript

Problem
I'd like to generate an AppleScript that perform the equivalent of "Start Speaking," which exists in the context menu of my Mavericks 10.9.2 system. Currently if I select an English selection, the OS will speak English.
I want to create an AppleScript that produces audio text-to-speech service but using Mandarin Chinese. I initially suspected that my language locale might have something to do with OSX responding in a particular language, but I am no longer certain (since changing locale on my system had no effect). I've looked into the issue and have found that the following preferences need to be set to enable the text to speech functionality. Once enabled I can select text in Safari and some other apps to use the Start Speaking Service. I am interested in the following approaches:
Use of defaults (can I use defaults to get OSX to recognize the
selected Chinese characters?)
Use of some other internal settings that can help me accomplish
text-to-speech (Chinese)
Use of an external link that will add the selected text to a query
string which can then be opened via Safari to a translation site
(I'm leaning more towards this option with translate.google.com)
Number (3) three seems promising and I'd appreciate if anyone could provide a practical use-case for any of the approaches above.
NB:
I've tried a few items already including setting up an Automator Service with speak text and an AppleScript to open a web location with the uri encoded data.
on run {input, parameters}
set targetURI to urlEncode(input)
open location "http://translate.google.com/#zh-CN/en/" & targetURI --input
end run
on urlEncode(str)
local str
try
return (do shell script "/bin/echo " & quoted form of str & ¬
" | perl -MURI::Escape -lne 'print uri_escape($_)'")
on error eMsg number eNum
error "Can't urlEncode: " & eMsg number eNum
end try
end urlEncode
Update
After incorporating the following instructions I was able to use the built in Speech > Start Speaking context menu in Safari/Notes to accomplish the needed functionality. Full credit and thanks for the info that led me to an appropriate solution.
Navigate to System Preferences > Dictation & Speech
Select the Text to Speech tab
Select the System voice popup button
Select Customize...
Search "chin" (no quotes)
Select the available Chinese voices (Sin-ji speaks Cantonese,
Ting-Ting speaks Beijing Mandarin, Mei-jia speaks Taiwanese
Mandarin)
Change the System Voice to the Appropriate Chinese dialect you want
In Safari and elsewhere, use the default context menu on highlighted
Chinese text and system will speak in the current Chinese dialect
selected

You just need to open automator and create a new service. Set the automator service to "service receives text from any application". Then add a "speak text" action. In the list of voices choose whatever language you want to hear. Hopefully you have a Chinese language speaking voice. Save it.
Now when you highlight some text, right-click on it, go to the services menu, and choose your automator service.

Related

Run Applescript on selected text and store it on clipboard

I'm looking for a solution to be able to select a text on an app(Figma) and then reverse it then store reverse version to the clipboard.
I used BetterTouchBar app for saving selected text but I can't do anything with it.
How can I do that with or without BetterTouchbar App.
Thanks a lot.
Create an Automator Quick Action1, setting Workflow receives current text in any application and add a Run AppleScript action, adding the following line of example AppleScript code where is says: (* Your script goes here *)
set the clipboard to the (reverse of characters of item 1 of input) as text
Save the Quick Action as e.g.: Reverse Selected Text To Clipboard
It's now available from the Services menu in any application, e.g. TextEdit > Services, or the Services menu on the Context menu (right-click menu).
You can also assign a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Services
1 In versions of macOS prior to Mojave a Quick Action is called a Service.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the used to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.

How to modify MacOS Dock shortcuts/hotkeys?

I want to modify/change/add MacOS Dock shortcuts/hotkeys.
e.g., of a shortcut that is available by default:
Option-Click on Dock app icon of an app that is not currently open = Hide the currently active app and then Open the app that was clicked
(from: https://support.apple.com/kb/PH21922?locale=en_US)
One very specific ability that I want:
Open and then Hide an app
Shift-Click on Dock app icon = Open and then Hide that app
(or use another easy modifier-key-combo with the click)
I am aware of the bash command open -a App --hide (e.g., open -a TextEdit --hide). I want to implement this exact functionality with a convenient Dock shortcut like the one mentioned above. If you're wondering "why?": sometimes I just want to open an app because I know that I will need it soon, but I'm still busy with another app, so just open this second app and then immediately hide it so it doesn't get in my way while I'm still busy with that first app.
How do I do this?
You can't.
These keyboard bindings are built into the Dock application, and cannot be modified.
The Mac utility program, Keyboard Maestro, does what you've asked, with shortcut keys, without using the Dock:
open, then immediately hide/minimize
option+open
I came up with a related solution: (in case anyone is interested)
an AppleScript App that presents a pick list
Method:
create a plain text document containing a list of the apps you want to handle (use correct name, no path, no extension, one name per line, no commas)
open 'Script Editor' (/Applications/Utilities)
copy-pasta the following code (and edit the first code line for the path to your text file from first step)
set apps_file to ("path:apps_list.txt")
set apps_list to paragraphs of (read file apps_file)
set apps_pick to choose from list apps_list with prompt "Select one or more apps." with multiple selections allowed
if result is false then return
set path_base to "Macintosh HD:Applications:"
set path_msft to path_base & "Microsoft Office 2011:"
set path_utly to path_base & "Utilities:"
set spec_msft to "Microsoft"
set spec_utly to "Activity Monitor, Terminal"
repeat with apps_this in apps_pick
if apps_this contains spec_msft
set path_this to path_msft
else if apps_this is in spec_utly
set path_this to path_utly
else
set path_this to path_base
end if
set apps_open to path_this & apps_this & ".app"
run application apps_open
end repeat
return
i. navigate menu 'File -> Export...'; ii. use the option 'File Format: Application'; iii. Save; (put the resulting app in your Dock)
Its not quite as convenient as I hoped, but, not too shabby.

How to use AutoIt to click a window dialog in a different language

On Windows 7, when we install an unsigned driver, it will pop-up a Windows security dialog. It is easy to have it be clicked on by AutoIt. Just search the window dialog with the specified window title.
But now, I want to use it on another platform with a different language. The window title will be changed to the local language, not the 'Windows security'.
How can I create the AutoIt script, which can handle the Windows security dialog in different language?
Fix this issue by finding a child handle from the security window class.
Try to use a class on Tittle. Title = [CLASS:notepad]
for more information visit here http://autoitsourcecode.blogspot.com/2013/04/autoit-control-send.html
Use the "AutoIt v3 Window Information" Tool provided by the "AutoIt full installation" and try to get some unique information like a Class name which is occuring in each different language dialog window as descirbed in advanced window descriptions. Then use these to search for the proper security window.
I'd suggest, you use the WinWaitActive("[REGEXPTITLE:Windows Security|Windows Sicherheit]") function with regular expressions for being able to wait for multiple windows with different titles.

OSX Application or Web App for converting text to plain text (unicode)

I am looking for ways to quickly converting blocks of text created in Word, etc. into plain text (i.e. turning right and left quotation marks into "plain text" quotation marks) for quickly transferring content to code with as few headaches as possible.
I came across this:
http://www.softpedia.com/get/Office-tools/Other-Office-Tools/Keith-Fenske-Plain-Text.shtml
...but it is Windows only and I prefer to dev on a Mac. Does anyone have a suggestion for an OSX tool or better yet a web app?
If you're using Snow Leopard, it's easy to create a Service to clean text. Run /Applications/Automator, choose the Service template, set it to receive text in any application, and enable replacing the selected text. Add a Run Shell Script action to the workflow, with Pass Input set to stdin. For the actual script, paste this in in place of the template (cat):
LC_CTYPE=en_US.UTF-8 tr '‘’‛❛❜“”‟❝❞‐–—­‒‑' "['*5]"'["*5][-*6]'
(note: hopefully all the various funny characters I included in the first string will pass through our various web interfaces intact... if not, edit the collections of quote marks to include whatever you need to squash in the first string, and matching numbers of their plain-text equivalents in the second string. And feel free to add other replacements as needed.)
Anyway, save this Service with some reasonable name, and then to invoke it just select some text (in any Cocoa app -- not, unfortunately, MS Word), and select your service from the application menu -> Services submenu. Also, you can use the Keyboard preference pane to assign it a keyboard shortcut if you like.
Text Wrangler from Bare Bones Software. This is BBEdit's free little brother (which will also do what you want).
The "Plain Text" Java application will run on Linux, Mac OS, and Windows.

How do I debug AppleScript?

What tips and tricks do you have for debugging AppleScript? Is there a debugger? If not, what is the best way to insert "prints" to display the value of variables? Is there a way to "pretty print" more complicated data structures?
The latest versions of xcode will let you create an AppleScript application but breakpoints in applescript don't work since Apple has discontinued support for AppleScript debugging in xcode.
Fallback: for simple "printf style" debugging you could use:
display dialog "my variable: " & myVar
Script Debugger
XCode
Getting the properties of an object (see below) to understand why it fails, when run from script editor. You can also use the class word, to see what class a property is. The Dictionary for an app is a good starting point.
One technique that often would have helped me, (and that I still sometimes do) is to tell something to return their properties, like this:
tell application "TextEdit"
get properties
end tell
Log statements and Console.app, for debugging of runtime events. (further below). You can ofcourse turn debugging on and off by setting a property
Below is a techniuqe I use for tracking runtime errors, in applets, mail rules, and what have you. When it fails, the error number and message is logged into TestDrive.log, and can be found in the left margin of Console.app…
tell application "TextEdit"
try
set a to text 0 of its name
on error e number n
my logit("OOPs: " & e & " " & n, "TestDrive")
end try
end tell
to logit(log_string, log_file)
do shell script ¬
"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit
If you're building any amount of AppleScripts, ScriptDebugger is the best tool I can recommend. Having said that...
Xcode is a free option that can be used to develop AppleScripts and can step through code with the debugger. The ability is primarily included so you can build Cocoa applications with AppleScript Studio, but you could use it for any AppleScript development.
If you're looking for something simpler, you might check out Smile, which isn't really a debugger, but does offer features useful for debugging that aren't available in the standard Script Editor.
If a display dialog is too small you can use TextEdit to show big returns:
tell application "TextEdit"
activate
make new document
set text of document 1 to myResults
end tell
Source http://forums.macrumors.com/showthread.php?t=446171
To get the names of windows and other GUI elements and properties, I've found UI Browser invaluable. You can use it to inspect whatever you want to control with AppleScript to find the designations of the elements you want to control
Not free, but easily worth it for a serious developer.
Use the log command. Example:
log "Hello world!"
The output can then be seen in the "Messages" windows in the official editor.
Reference

Resources