Apple Script won't display line breaks - dialog

Making a small script to connect me to the console on some cisco equipment via a usb to serial connection in terminal.
However I am finding it impossible to put line breaks or returns in between the text and Variables (like using <br> in HTML).
So in the display dialog line all the text comes out all mashed together.
I have tried using \n , \ \n, changing the preferences to Formatting > escape tabs and line Breaks in strings > Checked.
So any help would be much appreciated.
set theFind to "ls /dev/tty.*"
set theResult to do shell script theFind
set theConnection to text returned of (display dialog "Serial interface to availble:" & theResult & "Interface to use:" default answer "/dev/tty.usbserial")
tell application "Terminal"
activate
tell application "System Events"
delay 1
keystroke "screen " & theConnection & " 9600"
keystroke return
beep
end tell
end tell

AppleScript doesn't use linefeeds as line breaks (as unix generally does), it uses carriage returns. You can translate them with tr '\n' '\r' (suitably escaped, of course). Also, you'll need to manually add returns before & after the included output; for some bizarre reason, AppleScript uses \n for that instead of \r. Here's the net result:
set theFind to "ls /dev/tty.* | tr '\\n' '\\r'"
set theResult to do shell script theFind
set theConnection to text returned of (display dialog "Serial interface to availble:\n" & theResult & "\nInterface to use:" default answer "/dev/tty.usbserial")
tell application "Terminal"
activate
tell application "System Events"
delay 1
keystroke "screen " & theConnection & " 9600"
keystroke return
beep
end tell
end tell

Related

xmessage long text - wrap?

I want to use xmessage (it is the only available in the release I am using sorry) to display a message to the user. The message is a bit long and does not fit in one line. xmessage displays the message in one line up the point that is visible.
Is there any way to automatically wrap the text?
I tried the trick to
echo -e " message line 1 \n message line 2" | xmessage -file -
BUT because my message is coming from a variable and I want to also use it in my logger I do not want to use the "\n" in it. Do I have any chances?
You can pipe the text through fmt.

How to update applescript to open txt files in vim in iTerm3

For years I've been using the following AppleScript to open txt files in vim in iTerm2, but since iTerm 2.9.2 (aka iTerm3) it's broken. Could anyone advise how to update this AppleScript so it works again?
on run {input, parameters}
if (count of input) > 0 then
tell application "System Events"
set runs to false
try
set p to application process "iTerm"
set runs to true
end try
end tell
tell application "iTerm"
activate
if (count of terminals) = 0 then
set t to (make new terminal)
else
set t to current terminal
end if
tell t
tell (make new session at the end of sessions)
exec command ("vim \"" & POSIX path of first item of input as text) & "\""
end tell
if not runs then
terminate first session
end if
end tell
end tell
end if
end run
I originally copied the script from http://earthwithsun.com/questions/283418/how-can-i-make-terminal-vim-my-default-editor-application-in-mac-os-x but I have no AppleScript experience whatsoever.
Any help most appreciated!
B
Using #dusty's link, I recommend that you change the entire code section of your Run AppleScript Action to this:
on run {input, parameters}
tell application "iTerm"
activate
if (count of windows) = 0 then
set t to (create window with default profile)
else
set t to current window
end if
tell t
tell current session
write text ("vim \"" & POSIX path of first item of input as text) & "\""
end tell
end tell
end tell
end run
It seems to work on my machine, but I never use vim, so I am not sure if this will give you precisely what you want.
Good luck,
The new Applescript syntax doesn't have terminal as a top-level object like before. It has been changed to reflect more of the common pattern used throughout the OS for other scriptable applications: the top-level objects of the application are called windows, not terminals.
So the hierarchy is now something like this:
the application containing one or more
windows containing one or more
tabs containing a
session
https://iterm2.com/applescript.html has been updated with examples of the new syntax.
I adopted the code snippet of Craig Smith (great answer!) and tweaked it a little to open a new tab if there is already an open window. This way, you don't get any trouble if you have already something like vim opened in your iTerm. Also, this code changes the directory to the directory of the file to make NerdTREE and fuzzy finders like fzf.vim happy.
on run {input, parameters}
set filename to POSIX path of input
set cmd to "clear; pushd " & quote & "$(dirname " & filename & ")" & quote & " > /dev/null; nvim " & quote & "$(basename " & filename & ")" & quote & "; popd > /dev/null"
tell application "iTerm"
activate
if (count of windows) = 0 then
set t to (create window with default profile)
else
set t to current window
tell t
create tab with default profile
end tell
end if
tell t
tell current session
write text cmd
end tell
end tell
end tell
end run
I took inspiration in the previous anwers and made a few extensions.
My solution accepts multiple input files (e.g. from a selection).
If there is an active window, the script searches for open vim sessions in any of the tabs and opens the files inside of that vim instance. If there are none, it creates a new iTerm tab or window.
on run {input, parameters}
set vimCommand to "nvim -p "
tell application "iTerm"
activate
if (count of windows) = 0 then
set w to (create window with default profile)
else
set w to current window
try
# raises error when all windows are minimized
tell w
# look for tab with open vim session
repeat with t in tabs
tell t
if name of current session contains "vim" then
# open files in current vim session
set esc to character id 27
tell current session
write text (esc & esc & ":silent! tablast")
repeat with filename in input
set filePath to quoted form of POSIX path of filename
write text (":execute 'tabedit '.fnameescape(" & filePath & ")")
end repeat
select
end tell
select
return
end if
end tell
end repeat
# no existing session
create tab with default profile
end tell
on error msg
set w to (create window with default profile)
end try
end if
# open in new tab or window
tell w
tell current session
set launchPaths to ""
repeat with filename in input
set filePath to quoted form of POSIX path of filename
set launchPaths to launchPaths & " " & filePath
end repeat
write text (vimCommand & launchPaths)
end tell
end tell
end tell
end run
Something like this in Automator:
on run {input, parameters}
set vim_par to POSIX path of input
set cmd to "/usr/local/bin/vim " & quote & vim_par & quote
tell application "iTerm"
tell current window
create tab with default profile command cmd
end tell
end tell
end run
Save as .app, and then open files via it -- probably, make that .app a default "Open with" app.

AppleSctipt Finding File with partial filename

I am an applescript noob and i need help getting a certain log file that has a long string of numbers in its title. The number changes and the filename before the number string is unique to the folder it is in. Is there some way i can use the "starts with" to retrieve the file with only using part of the filename and make a variable to work with it?
This is best done with shell script. Use "do shell script" if you must do it in AppleScript. You can concatenate the string part of do shell script with whatever you want, for example:
set file_start to "file_start"
set file_ext to ".txt"
do shell script "mdfind -name " & quoted form of file_start & "|grep " & quoted form of (file_start & "[0-9]*" & file_ext & "$")
replace file_start in quotations with your file's non-changing beginning, and replace the extension with whatever you're using.
do shell command "find ~/some/directory -iname *somefile*"
This actually ended up working a little better.

Repeat over files in a File > Open dialog with Applescript

In the below script I use an Application other than Finder to launch an "Open" browser and perform a search in it.
I've got the window into the state I want, but whatever I try can't access the list of files to repeat over.
If anyone can help by adding the code to repeat over that file list and log out the file path of each file it'd be a huge help.
Thanks a lot.
tell application "Preview"
-- start the app
activate
-- let it boot up
delay 3
-- ensure it still has focus
activate
end tell
tell application "System Events"
tell process "Preview"
-- spawn "Open" window
keystroke "o" using {command down}
delay 0.5
-- spawn "Go to" window
keystroke "g" using {command down, shift down}
delay 0.5
-- expand scope of search to all of this mac
keystroke "/"
keystroke return
delay 0.5
-- spawn search field
keystroke "f" using {command down}
delay 0.5
-- perform search
keystroke ".jpg OR .jpeg"
keystroke return
end tell
end tell
Could you use mdfind or choose file instead?
mdfind kMDItemContentType=public.jpeg -onlyin /
osascript -e 'tell app "SystemUIServer"
choose file default location "/" with multiple selections allowed
end' &
sleep 1
osascript -e 'tell app "System Events"
keystroke "f" using command down
keystroke "kind:JPEG image"
keystroke tab & tab & tab & tab & tab & tab
delay 1
key code 125
delay 0.1
keystroke "a" using command down
keystroke return
end'

How can one put the output of a command into a konsole title bar?

Through the clever use of some escape characters, I used to put the output of arbitrary commands (e.g. "dirs") into my xterm title bar. Can I do the same thing in konsole? If so, how?
It's a little tricky to do what you want, but you can change Konsole's title bar. Go to:
Settings > Edit current profile > Tabs > Tab title format
and change it to %w which means Window Title Set by Shell. I think you need to close Konsole and reopen it for the changes to take effect.
Anyway, go to the prompt and exec:
OUTPUT=`whoami`; echo -ne "\033]2;$OUTPUT\007"
and behold!
This example sets the title of the window temporarily to whatever is outputted by whoami.
You can also do it using dbus:
qdbus $KONSOLE_DBUS_SERVICE $KONSOLE_DBUS_SESSION setTitle 1 $(dirs)
for KDE 3, using dcop:
dcop $KONSOLE_DCOP_SESSION renameSession $(dirs)

Resources