Repeat over files in a File > Open dialog with Applescript - dialog

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'

Related

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.

Apple Script won't display line breaks

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

Customizing zsh auto complete

I decided to give zsh a try. First i'll describe how things work right now and then i'll describe how I would like them to work.
Lets say I have 2 subfolders in current folder, 1st one "Documents", 2nd one "Downloads".
If I type "cd D" and press TAB, it will auto complete with "Do".
Press TAB again, it will auto complete with "Documents".
Press TAB again, it will auto complete with "Downloads".
Press TAB again, it will auto complete with "Documents" (I appreciate that here it dosen't go back to "Do" like Bash does).
At this point I have to pres Enter twice to change the directory. (first Enter to select "Documents" and second one to execute the comand).
How I would like zsh to behave:
If I type "cd D" and press TAB, I would like to auto complete with "Documents".
Press TAB again, should auto complete with "Downloads".
Press TAB again, should auto complete with "Documents".
Press Enter once and the directory should change to "Documents".
How can I do this? :-)
Wow, one year and no replies. I hope you found out how, but for others who might come across this via a search...
First make sure you have the completion module loaded with this line
zmodload zsh/complist
Then you can bind Enter, aka ^M, during menuselect to the function accept-line - which is normal behaviour - but by prefixing it with a dot, if forces it to leave menuselect mode before the function executes
bindkey -M menuselect '^M' .accept-line

How to open/toggle to the 10th screen session in linux?

I have a screen session where in i have created 10 sessions .
I use the below key combination to toggle to the active screen session attached.
ctrl + a + number 0 to 9
Ctrl-a 0-9 Go to a window numbered 0-9
i have created a 10th session now
ctrl-a 10 will end up to the screen session 1.
how to toggle to the 10th session ??
work around for the same is , go to the 9th session
Ctrl-a 9
and then ctrl-n will lead me to the 10th session.
Link i referred to learn screen in linux
http://www.kb.indiana.edu/data/acuy.html
Pressing Ctrl+A then ' (apostrophe) will display a prompt and let you enter the number or name of a window to switch to.
To rename a window, use Ctrl+AShift+A. (Ctrl+U to remove the old name before typing a new one.)
Other ways to switch windows include:
Ctrl+ACtrl+A to return to the window you were looking at before
Ctrl+An or Ctrl+ASpace to go to the next in sequence
Ctrl+Ap to go to the previous in sequence
Ctrl+A" to select from a list (thanks stan and dogbane!)
I think some of these can open a windows list:
ctrl+a " #window list
ctrl+a w #window list
You can use C-a " which will show you a list of all windows and you can then select the 10th one
Alternatively, go to the 9th window and then use C-a n to go to the 10th.
According to screen's manual page, you can add the following lines to your ~/.screenrc file:
bind -c demo1 0 select 10
bind -c demo1 1 select 11
bind -c demo1 2 select 12
bindkey "^B" command -c demo1
makes C-b 0 select window 10, C-b 1 window 11, etc. Alternatively, you can use:
bind -c demo2 0 select 10
bind -c demo2 1 select 11
bind -c demo2 2 select 12
bind - command -c demo2
makes "C-a - 0" select window 10, "C-a - 1" window 11, etc.

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