I would like to know how to get the current location of the current session in iTerm using Applescript.
I have tried all sorts of things. The dictionary is very difficult for me to understand.
Here is my latest attempt
tell application "iTerm"
tell the current session of current terminal
set theSelection to attachment file name
display dialog theSelection
end tell
end tell
And the error this produces is:
error "iTerm got an error: Can’t get file name of current session of current terminal." number -1728 from file name of current session of current terminal
I just want the location the iTerm session is currently in: /Users/SuperCoolGuy/Desktop
This is ugly...
tell application "iTerm"
if not (exists terminal 1) then reopen
set mySession to current terminal's current session
tell mySession
write text "pwd"
set sessionContents to it's text
end tell
end tell
set myPath to paragraph -2 of (do shell script "grep . <<< " & quoted form of sessionContents)
If somebody stumbles upon this question, there's another option now with Shell integration. With it you can access the path in AppleScript using the iTerm defined variable session.path:
activate application "iTerm"
tell application "iTerm"
tell current session of current window
set myTerm to (variable named "session.path")
write text "echo this is the current path: " & myTerm
end tell
end tell
That information isn't currently available through iTerm's AppleScript interface. The only exposed properties of the session object having to do with what's running in it (as opposed to its visual appearance) are contents and tty, and those aren't what you're looking for here.
Related
I need the applescript to run tabset test command to change the name of the current tab of iTerm.
-- Launch iTerm and log into multiple servers using SSH
tell application "iTerm"
activate
create window with default profile
set newWindow to (create window with default profile)
set Servers to paragraphs of (do shell script "/bin/cat $HOME/serverlist")
repeat with nextLine in Servers
if length of nextLine is greater than 0 then
tell current window
create tab with default profile
tell current session of newWindow
do shell script "export PATH='/usr/local/bin:$PATH'; tabset test "
end tell
end tell
end if
end repeat
tell first tab of current window
close
end tell
tell second window
close
end tell
end tell
The problem is tabset test does not work, and there is no any error prompted.
The tabset command could be installed via npm install -g iterm2-tab-set
Ted Wrigley is right. I should use iTerm's write command to type the text into the window.
Probably a minor syntax issue that I'm getting wrong, but can't find the solution in the ITerm2 documentation. I'd like to create an applescript that opens an ITerm window with three tabs, each running various shell commands (ls, cd, echo, etc.) with the tab the remaining open after those commands have run. The opening tabs part is working fine, but it appears that as soon as the commands run, the tab closes (if I don't provide any commands, the tab will remain open.) For my script here:
tell application "iTerm2"
create window with default profile
tell current window
create tab with default profile command "echo abc"
create tab with default profile
end tell
end tell
Instead of "echo abc" what should I put there so the echo command will run in the tab, but leave me with a cursor for me to type in more commands instead of the tab immediately closing thereafter?
Instead of using the create tab ... command, use a separate write text command. For example, this is a script I use to open a terminal to a specific directory:
tell application "iTerm"
create window with default profile
tell current session of current window
write text "cd " & directory & "; clear"
end tell
end tell
Using the "write text" suggested by whereswalden I settled on the following, works well:
tell application "iTerm2"
create window with default profile
tell current window
tell current session
write text "echo abc"
end tell
create tab with default profile
tell current session
write text "ls -la"
end tell
create tab with default profile
tell current session
write text "cd mydir"
end tell
end tell
end tell
I was trying to use a script to open CS:GO with an AppleScript program, but I get the error message:
Handler cannot handle objects of this class -10010
Here's the script:
set myfilepath to "Macintosh HD:Users:Kasper:Desktop:Counter-Strike Global Offensive"
tell application "Finder" to open myfilepath
delay 5
tell application "System Events" to set unixID to unix id of process "csgo_osx"
do shell script ("renice -20 -p " & unixID) password "2306" with administrator privileges
Any ideas why this happens?
Your second line is telling Finder's Open command to open a string. It needs to be given a file or alias. Change that line to:
tell application "Finder" to open file myfilepath
I would like to be able execute an AppleScript command (from a file) that will open up new tabs for specific directories.
What would be the best way to do this?
Right now I have a node.js script that I loop through each dir and pass the dir to this AppleScript file:
on run arg
set p to arg's first item
set g to "cd " & p & "; clear; pwd"
tell application "iTerm"
make new terminal
tell the current terminal
activate current session
launch session "Default Session"
tell the last session to write text g
end tell
end tell
end run
However, that is not doing what I like (it opens the right amount of tabs, but the last one gets everything written to it).
BONUS: if you can show me how to make the original tab active after opening all the tabs.
tell application "iTerm"
if exists current terminal then
set t to current terminal
else
set t to make new terminal
end if
tell (launch session "Default Session") of t to write text "cd /etc;clear;pwd"
tell (launch session "Default Session") of t to write text "cd /var;clear;pwd"
activate
end tell
This question may seem strange. However I'm in the need of interacting with an application that is releasing worldwide with different names, eg. AppEN, AppGB, AppDE etc...
I am looking for a solution that allow me to use this command:
tell application process "AppnameGB"
However it should work with all the different variations of this application. I don't know if this is possible but searching for a string in the application name could do the trick: tell application process that contain in its name "Appname".
If the process is already open, you can use something like this:
tell application "System Events"
tell (process 1 where name starts with "TextEd")
properties
set f to its file
end tell
end tell
tell application (f as text)
properties
end tell
Telling Finder to list files is really slow:
tell application "Finder"
item 1 of (path to applications folder) where name starts with "Text"
end tell
You can use do shell script though:
set a to do shell script "ls /Applications/ | grep -m1 '^Text.*\\.app$'"
tell application a
properties
end tell
set a to do shell script "mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName==Text*' | head -n1" would also search outside the applications folder.
The easiest is if you could use the bundle identifier of the application instead of its name. Most likely the bundle ID doesn't change while the name does. So use this script on that application and check if the bundle ID changes or not.
tell application "Finder" to set bundleID to id of (choose file)
If you find it doesn't change then you can access it via applescript like this...
tell application "com.bundle.id"
activate
-- do something
end tell
Your only other alternative is get a list of all applications, and loop through them checking the name as you suggest. Something like this wold work but is pretty slow.
-- use spotlight to find all the apps on the computer
set cmd to "mdfind 'kMDItemContentType == \"com.apple.application-bundle\"'"
set foundApps to paragraphs of (do shell script cmd)
-- search the found apps looking for the one you're interested in
set appPath to missing value
repeat with i from 1 to count of foundApps
if (item i of foundApps) contains "Appname" then
set appPath to item i of foundApps
exit repeat
end if
end repeat
if appPath is missing value then error "Couldn't find the app!"
-- open the app
set macAppPath to (POSIX file appPath) as text
tell application macAppPath
activate
-- do something
end tell