There is an article (https://medium.com/#mrdoro/fast-translation-with-google-translator-and-mac-osx-817e32233b7a) from Lukasz Dorosz
about using Apple Automator to integrate Google translator with macOS. I did it and it works. My question is - How can I integrate DeepL translator with macOS?
Using Automator you can integrate a Goole Translator with macOS in few steps:
Open an Automator and create a new Service.
The top section set in this way:
From the left column you need to find and grab two functions: Run >Apple Script and Website Popup.
Copy and Paste this code into Apple Script window.
on run {input, parameters}
set output to "http://translate.google.com/translate_t?sl=auto&tl=ru&text=" & urldecode(input as string)
return output
end run
on urldecode(x)
set cmd to "'require \"cgi\"; puts CGI.escape(STDIN.read.chomp)'"
do shell script "echo " & quoted form of x & " | ruby -e " & cmd
end urldecode
How do I change the script to use the translator from DeepL instead of Google?
You can see the DeepL link format by clicking the share button. The links look like this:
https://www.deepl.com/translator#de/en/Dies%20ist%20kein%20Haus.
So your script should look something like this:
on run {input, parameters}
set output to "https://www.deepl.com/translator#de/en/" & urldecode(input as string)
return output
end run
on urldecode(x)
set cmd to "'require \"cgi\"; puts CGI.escape(STDIN.read.chomp)'"
do shell script "echo " & quoted form of x & " | ruby -e " & cmd
end urldecode
If the text is long enough, the source language in the link should also be irrelevant, since the DeepL language recognizer determines the actual language.
The DeepL app that #tecmec recommended in his comment is probably much better than any script.
Related
I'm trying to create a script that asks the user what .mp3 they want to split using Terminal through AppleScript / Script Editor. Here's what I have so far.
I am trying to get input to display in this line:
do script "spleeter separate -i (text returned of input).mp3 -p spleeter:2stems -o output"
in terminal, it just says that there are no matches found for (text returned of input).
Here's my code:
set input to display dialog "What would you like to convert?" default answer "" with icon note buttons {"Continue", "Cancel"} default button "Continue"
tell application "Terminal"
do script "cd Desktop/spleetr"
do script "spleeter separate -i (text returned of input).mp3 -p spleeter:2stems -o output"
end tell
You are placing a command inside a string, so it is not getting evaluated. The solution for that is to concatenate the results of the command and the string parts in the desired order.
When using the Terminal, unless specified otherwise, each do script command is run in its own window/tab. If you don't neccessarily need a Terminal window, the do shell script command can be used instead (note that it uses a default shell, so you should use full paths), but to use multiple commands with either one you need to combine the various shell commands into a single statement, otherwise they will be run in separate shells.
I don't have that utility to test, but in the following script I am getting the text in the dialog statement, and quoting the result for the shell script in case it contains spaces, etc:
set input to text returned of (display dialog "What would you like to convert?" default answer "" with icon note buttons {"Continue", "Cancel"} default button "Continue")
-- to run in a new Terminal window: --
tell application "Terminal"
do script "cd Desktop/spleetr; spleeter separate -i " & quoted form of (input & ".mp3") & " -p spleeter:2stems -o output"
end tell
-- or if Terminal is not needed: --
do shell script "cd Desktop/spleetr; spleeter separate -i " & quoted form of (input & ".mp3") & " -p spleeter:2stems -o output"
I have several hundred lengthy applescripts to edit where I need to find and replace the following code snippet in various places in each script.
tell application "Adobe Photoshop CC 2015.5"
set myLayer to current layer of current document
if last character of mySport is "s" then
set contents of text object of myLayer to mySport & ""
else
set contents of text object of myLayer to mySport & "'s"
end if
end tell
I want to replace it with
tell application "Adobe Photoshop CC 2015.5"
set myLayer to current layer of current document
set contents of text object of myLayer to mySport & "'s"
end tell
Is there a way to write an applescript to find and replace several lines?
code screen grab
The second problem is how do I deal with the apostrophe contained inside the quotes?
You can probably tell that I'm an artist and not a developer or scripter! I tried to get an answer a while back but unsuccessfully and the problem is now become critical.
Many thanks in anticipation of an answer.
The best would have been to set this subroutine as a separate script library and call it it in each of your scripts. Doing so, only one change would be enough. I advice you to do this way for next time.
I dig to find a way to make change in a script, but that's not that easy. Script Editor as very limited capability for scripting. the work around is to use the GUI scripting, which means that any changes made by Apple in future versions may no longer work.
The script bellow simulate your keyboard action to search & replace CurString to NewString :
set myScript to "Users:imac27:Desktop:Testscript.scpt" -- path to your script
set CurString to "Set B to 2"
set NewString to "Set X to 5"
tell application "Script Editor"
open myScript
activate myScript
delay 2
tell application "System Events"
keystroke "f" using {option down, command down} --mode search & replace
keystroke tab using {shift down} -- got to search area
keystroke CurString -- set the search target
keystroke tab -- goto replace area
keystroke NewString -- set replace value
-- click on menu "Replace all " which is the 7th item of "Search" menu item (=item 14th of menu "Edit")
tell process "Script Editor" to click menu item 7 of menu of menu item 14 of menu 4 of menu bar 1
end tell
compile front document
save front document
close front document
end tell
This script opens the script, it does the search, replaces, clicks on "replace" menu, then it compiles new version, saves it and closes it. If you have many scripts, you must run it through a loop for each script.
I tested it OK with simple line : replace "Set B to 2" by new line "Set X to 5".
However, your issue is more complex because you want to replace several lines, not only 1. I did not found a way to set the search area with multiple lines. I tried with CR (13) or LF (10), but it does not work. May be someone has an idea for that part ?
Also, if you want to add a " in your search or replace patterns, you can use the following :
set Guil to ASCII character 34
Set CurString to "this is a " & Guil & "s" & Guil & " between quotes"
In this case, the CurString value will be : this is a "s" between quotes
I purchased Script Debugger from Late Night Software and it enables the script to access pieces of code and replace them. Mark Alldritt was amazing in the support he offered and the software is now my "first use" destination.
You are sure of your original script and the final script? In this case no hesitation to use xxd and sed below in hexadecimal script which you wrote you can test this script, no danger for your script. Naturally, you change your path and names at your convenience.
set thePath to POSIX path of (choose file)
tell application "Script Editor"
set doc to open thePath
save doc as "text" in POSIX file "/Users/yourname/Desktop/yourscriptold.txt"
close thePath
end tell
set scp to do shell script "xxd -p -c 100000 /Users/yourname/Desktop/yourscriptold.txt " & " | sed -e 's#74656c6c206170706c69636174696f6e202241646f62652050686f746f73686f7020434320323031352e35220a736574206d794c6179657220746f2063757272656e74206c61796572206f662063757272656e7420646f63756d656e740a6966206c61737420636861726163746572206f66206d7953706f727420697320227322207468656e0a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f727420262022220a656c73650a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f7274202620222773220a656e642069660a656e642074656c6c#74656c6c206170706c69636174696f6e202241646f62652050686f746f73686f7020434320323031352e35220a736574206d794c6179657220746f2063757272656e74206c61796572206f662063757272656e7420646f63756d656e740a73657420636f6e74656e7473206f662074657874206f626a656374206f66206d794c6179657220746f206d7953706f7274202620222773220a656e642074656c6c#' > /Users/yourname/Desktop/yourscriptnew.txt"
set scp to do shell script "xxd -r -p /Users/yourname/Desktop/yourscriptnew.txt >/Users/yourname/Desktop/yournewscript.txt"
do shell script "osacompile -o " & "/Users/yourname/Desktop/temporyname.scpt" & " /Users/yourname/Desktop/yournewscript.txt"
do shell script "rm -f /Users/yourname/Desktop/yourscriptold.txt "
do shell script "rm -f /Users/yourname/Desktop/yourscriptnew.txt "
do shell script "rm -f /Users/yourname/Desktop/yournewscript.txt "
I have apple script which is working perfectly fine, until I add use script “Alert Utilities” at the start. Then it gives Expected end of line, etc. but found identifier. error at set md5 to do shell script "md5 -q " & quoted form of myFile with shell highlighted. What could possible be causing this error?
Add use scripting additionsand the code will run fine. Since AppleScript 2.3 (Mavericks) there is the new use statement. It will tell the neccessary resources that are required to run the script like which applications (optional), script libraries, cocoa frameworks in AppleScriptObjC or if it will use scripting additions or not. For backward compatibility when the use statement is not used, by default scripting addition are loaded. If the use statement is used, the scripting additions are by default not loaded.
So your script will look like:
use script “Alert Utilities”
use scripting additions
-- continue code
set md5 to do shell script "md5 -q " & quoted form of myFile
Add use "scripting additions" statement. use statement automatically disables scripting addtions such as display dialog.
also,
set md5 to (do shell script "md5 -q " & quoted form of myFile with shell)
I executed the terrminal commnand "system_profiler SPApplicationsDataType" in AppleScript for to obtain installed app in MAC, I need to parse the output, Pls explain how to achieve it either thru Apple script or Python.
In Python you can use subprocess -
http://docs.python.org/library/subprocess.html
Specifically, the check_output method: http://docs.python.org/library/subprocess.html#subprocess.check_output
So you would just use:
output = subprocess.check_output(["system_profiler", "SPApplicationsDataType"])
print output # or do something else with it
Your command will be broken into an array, one element for each of your arguments.
In Applescript you could do something like:
set textReturned to (do shell script "system_profiler SPApplicationsDataType")
tell application "TextEdit"
activate
make new document at the front
set the text of the front document to textReturned
end tell
This will run the command and dump the output to TextEdit.
I would like to make sure that an applescript can be converted to bash. Are there any ideas on how to do this? And if so, I'll place a simple applescript below to give you an example of how the script runs. In more clarity, I simply want a bash script or shell script to do what my applescript is doing. I want it to "enable" or change the default of the switch in system preferences, under "energy saver" that reads ...'start up automatically after a power failure'...:
set uiScript to "click checkbox \"Start up automatically after a power failure\" of list 2 of group 1 of window \"Energy Saver\" of application process \"System Preferences\""
run script "tell application \"System Events\"
" & uiScript & "
end tell"
any ideas on how to convert this script?
Thanks,
-Unimachead
You may not actually need to convert it - you can run AppleScript from within a bash script, using osascript.
$ man osascript
Note that you can run AppleScript either from a file or just include the source on the command line.