Applescript conversion to Bash - bash

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.

Related

Replace text in an applescript using an applescript

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 "

Switching between two Mac terminal windows

I have an interactive bash script running in a Mac OSX bash Terminal window. I would like, from within that script, to
open a second Terminal window, print in it the content of a variable from the script in the first window,
keep that second window open somewhere on the screen while I continue interacting with the first window, and finally
have the second window closed when I do not need it anymore.
Since I am on Mac OSX, I am thinking of using osascript to run Applescript commands opening the second window, pasting the variable content in it and returning control to the first window, but I cannot make it work.
#!/bin/bash
var2print="I want this to print in the text window"
osascript -e '
tell application "Terminal"
tell window 1 # this just renames the first window
set custom title to "Main window"
end tell
do script # this opens a new window
tell window 1
set custom title to "Text window"
set selected to true # my first idea to put focus on this window
activate # my second idea to put focus on this window
end tell
end tell
'
printf "%s\n" "$var2print" # prints in main window, despite all efforts
read -sn 1 -p "Press any key to continue..."
Surprisingly to me, the very last command 'read' also takes place in the main window, but the focus is on the text window and I have to manually select the main window to press a key and end the script.
I have considered letting go of AppleScript and using the gnu-screen command instead, but it seems like overkill for my purpose to simply have some info displayed for a while.
Any help to better understand what's going on and to find a practical solution to switch between terminal windows would be greatly appreciated. W.
You can toggle two windows in Terminal.app with AppleScript this way
tell application "Terminal"
set index of window 2 to 1
end tell
window 1 is always the frontmost window
How about using a dialog box to display your message and go away automagically after a few seconds like this:
#!/bin/bash
bashvar="ZippedyDooDah"
osascript >/dev/null 2>&1 <<EOF
tell application "System Events" to display alert "$bashvar" buttons {"OK"} as warning default button "OK" giving up after 5
EOF
You can do it like this:
osascript -e 'tell app "Terminal" to do script "echo hello"'
Or, you can set a bash variable like this and send that for display:
MSG="FreddyFrog"
osascript<<EOF
tell app "Terminal"
do script "echo $MSG"
end tell
EOF
If you want to send it more than one message, you could make it tell you its tty as the command you pass, then you will have that in a file and you can send it further messages...
osascript -e 'tell app "Terminal" to do script "tty > tty.txt"'
If you now look in the file tty.txt you will see the device special file of the terminal window you created, some thing like /dev/ttys002, then you can do this from your original window
echo "Some stuff" > /dev/ttys002
echo "More stuff" > /dev/ttys002
or, more succinctly
echo hello > $(cat tty.txt)

Invoke copy & paste commands from terminal

Is is possible to invoke a copy command (as if the user pressed Cmd+C) from a bash script? Basically I want to write a simple script that I run with a global hotkey and it should take the current selection from the active app, replace something and paste the result. Is this possible?
The best I could come up so far is using pbpaste and pbcopy, but I'd like to automate that if possible.
If you're just trying to modify a text selection, you could use AppleScript.
osascript -e 'try
set old to the clipboard
end try
try
delay 0.3
tell application "System Events" to keystroke "c" using command down
delay 0.2
set text item delimiters to linefeed
set input to (paragraphs of (the clipboard as text)) as text
set the clipboard to do shell script "shopt -u xpg_echo; echo -n " & quoted form of input & " | rev" without altering line endings
tell application "System Events" to keystroke "v" using command down
delay 0.05
end try
try
set the clipboard to old
end try'
The first delay is for releasing modifier keys if the script is run with a shortcut that has other modifier keys than command. The second delay could also be reduced to something like 0.05, but long selections or for example web views often need a longer delay. Without the third delay, the clipboard would sometimes be set to old before the text would get pasted.
the clipboard as text and do shell script convert line endings to carriage returns by default. shopt -u xpg_echo is needed because the echo in sh interprets backslashes inside single quotes by default. If the input is longer than getconf ARG_MAX bytes, you can't use echo and have to either write it to a temporary file or use pbpaste.
pbpaste and pbcopy replace non-ASCII characters with question marks by default in the environment used by do shell script You can prevent that by setting LC_CTYPE to UTF-8.
Telling System Events to click menu bar items would often be even slower, and it wouldn't work in applications that don't have a menu bar or in full screen windows.
Another option would be to create an Automator service. But they also have small delays before they are run. There's a bug where the shortcuts for services don't always work until the services menu has been shown once on the menu bar. And the services aren't available when the frontmost application doesn't have a menu bar or a services menu.

How can I make my Applescript 'do' a script in my custom window?

I'm working on writing an Applescript that gets my Terminal ready for me to make Firefox add-ons.
tell application "Terminal"
do script "cd Public/addon-sdk-1.0"
do script "source bin/activate"
do script "clear"
end tell
When I run this script, my custom Terminal opens along with a regular Terminal window; and the bash script is ran in the regular window.. So, I'm trying to find out how to make the Applescript only open my custom Terminal, and execute the bash script in it.
The answer to your problem is to not use do script but to send keystrokes to your current terminal window with either keystroke or key code. Here's a script I use to do something similar. I just call this from the terminal with osascript myscript.scpt or launch it directly (I use LaunchBar for invoking applescripts) and it opens a new terminal tab (if the terminal is already open), gives it a custom name and then runs whatever commands I feed it. You could modify this to skip creating a new tab and just run in the current terminal window. I only use this approach when I have to do more than just run some standard terminal commands (such as send keys to an interactive python session), otherwise I just create a bash script.
global ENTER_, ESC_
set ENTER_ to 52
set ESC_ to 53
on run_commands(commands, pause)
tell application "System Events"
repeat with cmd in commands
keystroke cmd
key code ENTER_
delay pause
end repeat
end tell
end run_commands
on new_terminal_tab(tab_name)
activate application "Terminal"
delay 0.5
tell application "System Events"
# create new tab
keystroke "t" using {command down}
delay 0.5
# give it a name
keystroke "I" using {shift down, command down}
keystroke tab_name
delay 0.5
key code ESC_ # escape
end tell
end new_terminal_tab
new_terminal_tab("addon-sdk-work")
run_commands( { "cd /Users/username/Documents/dev/projname",¬
". env/bin/activate", ¬
"clear"}, 0.5)

osascript / syntax error: Expected end of line but found command name. (-2741)

I'm running into problems with a shell script that utilizes a small portion of Applescript. When I compile it with Applescript editor it works. It does not though within a shell script.
44:49: syntax error: Expected end of line but found command name. (-2741)
23:28: syntax error: Expected end of line but found “after”. (-2741)
Here is the shell code:
osascript -e 'tell application "System Events" -e 'activate'
osascript -e 'tell process "Application 10.5" -e 'set frontmost to true' -e 'end tell'
osascript -e 'delay 1' -e 'keystroke return' -e 'delay 1' -e 'keystroke return'
end tell
Applescript (that works):
tell application "System Events"
activate
tell process "Application 10.5"
set frontmost to true
end tell
delay 1
keystroke return
delay 1
keystroke return
end tell
[updated] / [solved]
This took care of any kind of problems I was having trying to modify the applescript to work within a shell script:
## shell script code
echo "shell script code"
echo "shell script code"
## applescript code
osascript <<EOF
tell application "Scriptable Text Editor"
make new window
activate
set contents of window 1 to "Hello World!" & return
end tell
EOF
## resume shell script...
It's very cool that you're able to put pure applescript directly into a shell script. ;-)
Each osascript(1) command is completely separate process, and therefore a completely separate script, so you can’t use state (such as variables) between them. You can build a multi-line script in osascript using multiple -e options -- they all get concatenated with line breaks between them to form the script. For a sufficiently long script, a separate file or a “here document”, as you used in your eventual solution, is a good way to go.
Also, if your script is mostly (or entirely!) AppleScript, you can make a “shell” script that simply is AppleScript using a shebang file that invokes osascript:
#!/usr/bin/osascript
display dialog "hello world"
...and then use do shell script as necessary.
Instead of using the -e flag, you can simply store your applescript code in a small text file and call
osascript /path/to/script
Also, if you're telling an application or process to do just one thing, you can write it like this:
tell process "MyProcess" to perform action.
Now that I think about it, running each line separately with the -e flag probably won't work because I don't think all the lines will connect and run as one program. For example, I just tested using osascript -e to set a variable. I then used a separate osascript -e to read the variable, and it couldn't.
[*]

Resources