reload Terminal preferences .plist after change - terminal

I used plutil to edit the plist. then I placed in under /User/<myUsername>/Library/Preferences/com.apple.Terminal.plist. I want it to update the settings without reboot the application. How do I do this? using defaults read com.apple.Terminal doesn't seem to make the changes take effect. I added a terminal profile to the plist and want the changes to take effect.
I tried making an import profile with AppleScript but long story short it's not 100% possible. if you store window 1 so it's the same var in-between delays right after open it errors more times.
on run argv
set importScript to first item of argv
set flag to application "Terminal" is not running
tell application "Terminal"
open importScript
delay 1.0E-5
activate
if flag then close back window
do script "exit" in window 1
delay 0.5
close window 1
end tell
end run

there is no need for editing plists just a simple AppleScript. the limitations of this are you must have _oti_<yourProfileId> in your terminal profile default header and change the title when running your terminal profile to prevent accidental closures when installing the same profile of an already running app
import.applescript
on run argv
set importScript to first item of argv
set closeScript to second item of argv
set profileId to third item of argv
do shell script "open -a Terminal " & importScript
do shell script "osascript " & closeScript & " _oti_" & profileId
end run
closeMe.applescript
on run argv
set c to first item in argv
tell application "Terminal"
set wList to every window
repeat with app_window in wList
set wname to name of app_window
if wname contains c then
do script "exit" in app_window
delay 0.312
close app_window
end if
end repeat
end tell
end run
and then you can call the profile using this AppleScript
on run argv
set flag to application "Terminal" is not running
set scpt to first item in argv
set n to second item in argv
set p to third item in argv
tell application "Terminal"
set newTab to do script scpt
set badFlag to back window is equal to window 1
if p is not equal to "" then set current settings of newTab to settings set p
set custom title of newTab to n
activate
if flag and (not badFlag) then
do script "exit" in back window
delay 0.1
close back window
end if
end tell
end run

Related

Applescript does not run user command

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.

Applescript to open terminal, run command, and show - Not working

I´m trying to create a keyshortcut to open terminal in current folder. Looking around, I found this code to create a service (the part of adding the shortcut to this service is solved), only added things are the "; clear" and some of the "activate" so it shows
on run {input, parameters}
tell application "Finder"
activate
set myWin to window 1
set theWin to (quoted form of POSIX path of (target of myWin as alias))
tell application "Terminal"
activate
tell window 1
activate
do script "cd " & theWin & ";clear"
end tell
end tell
end tell
return input
end run
It is not working as i would like.
troubles:
it opens two windows in terminal, have no idea why. It has nothing to
do with the added "activate"… it has always donde that
if I select an item on finder ( a folder ) it opens its parent directory and i would
like it to open the selected folder
this is my very first try with Applescript so if the error is obvious i just can't see it
Thanks in advance
The do script command already opens a window in Terminal. Try it this way:
tell application "Finder" to set theSel to selection
tell application "Terminal"
set theFol to POSIX path of ((item 1 of theSel) as text)
if (count of windows) is not 0 then
do script "cd " & quoted form of theFol & ";clear" in window 1
else
do script "cd " & quoted form of theFol & ";clear"
end if
activate
end tell
I like the reopen approach better...
tell application "Finder" to set currentFolder to target of front Finder window as text
set theWin to currentFolder's POSIX path
tell application "Terminal"
if not (exists window 1) then reopen
activate
do script "cd " & quoted form of theWin & ";clear" in window 1
end tell

Opening multiple windows with Applescript

I am trying to write an Applescript to open three VLC windows in different screen positions. The script opens three instances of VLC but has them one on top of the other (using the position for window 1). Help with the code appreciated:
do shell script "open -n /Applications/Video/VLC.app"
tell application "System Events"
activate
set bounds of first window of application "VLC" to {13, 36, 790, 519}
end tell
do shell script "open -n /Applications/Video/VLC.app"
tell application "System Events"
activate
set bounds of second window of application "VLC" to {13, 544, 790, 1027}
end tell
do shell script "open -n /Applications/Video/VLC.app"
tell application "System Events"
activate
set bounds of third window of application "VLC" to {13, 1043, 790, 1526}
end tell
#khagler's comment provides the right pointer: the VLC instances must be distinguished by their PIDs (process IDs; called unix id in AppleScript) in the System Events context.
The code below should do what you want, arrived at after much toil and trouble -- par for the [AppleScript obstacle] course. One obstacle was that the VLC instances' main windows do not get created right away.
The comments provide more details.
Note that because user-interface elements are programmatically manipulated, the application running your script must be granted assistive access for security reasons.
Note that I'm starting the instances with do shell script "open -na VLC.app", relying on the location of the app being known to Launch services (should that not work for some reason, revert to your method of specifying the full path).
# Specify the desired window bounds.
# !! In the "System Events" context, windows do not
# !! have `bounds` properties, but separate `position` and
# !! `size` properties.
set WIN_POSITIONS to {{13, 36}, {13, 544}, {13, 1043}}
set WIN_SIZES to {{790, 519}, {790, 519}, {790, 519}}
# Launch the VLC instances.
repeat with i from 1 to count of WIN_POSITIONS
do shell script "open -na VLC.app"
end repeat
# Note:
# Instance-specific manipulation must
# be performed in the "System Events" context, because
# we must distinguish the VLC instances by their
# PIDs (process IDs; called `unix id` in AppleScript).
tell application "System Events"
# Get the PIDs (process IDs) of all VLC instances.
set vlcPids to get the unix id of every process whose name is "VLC"
# Loop over all instance PIDs.
# !! It is imperative to *continue* to use object specifiers
# !! with *filters based on the PID* so as to ensure that the
# !! individual instances are targeted.
# !! Attempting to store references to these instances in
# !! variables fails subtly, as evidenced by the "Events"
# !! tab in AppleScript editor later showing the non-specific
# !! process "VLC" of application "System Events" specifiers.
set winNdx to 1
repeat with vlcPid in vlcPids
# WAIT for each instance to create its main window, wich
# sadly, is not available right away.
# Once created, position it.
set haveWin to false
tell (first process whose unix id is vlcPid)
repeat with i from 1 to 25 # times out after 25 * .2 == 5 secs.
if (count of windows of it) > 0 then
set haveWin to true
tell front window of it
# !! In the "System Events" context, windows do not
# !! have `bounds` properties, but separate `position` and
# !! `size` properties.
set position to item winNdx of WIN_POSITIONS
set size to item winNdx of WIN_SIZES
end tell
exit repeat
end if
delay 0.2 # no window yet; sleep some and try again
end repeat
end tell
if not haveWin then error "VLC instance " & vlcPid & " unexpectedly did not create a window within the timeout period."
set winNdx to winNdx + 1
end repeat
end tell
How to make this work with Finder:
Targeting Finder changes the approach for two reasons:
there's only one Finder instance.
you cannot open multiple windows with open -na Finder.app; thankfully, this answer shows how to do it (see the comments there for quirks).
Note that the following blindly opens additional Finder windows.
set WIN_POSITIONS to {{13, 36}, {13, 544}, {13, 1043}}
set WIN_SIZES to {{790, 519}, {790, 519}, {790, 519}}
# Sample target locations for the Finder windows.
# Note the use of the "System Events" context to faciliate use of
# POSIX-style *input* paths; note, however, that the paths are
# *stored* as HFS paths so that Finder accepts them.
tell application "System Events"
set WIN_TARGETS to {¬
path of desktop folder, ¬
path of folder "~/Downloads", ¬
path of folder "/Library/Audio"}
end tell
set winCount to count of WIN_POSITIONS
# Launch the Finder windows.
tell application "Finder"
# Create the windows in reverse orders.
repeat with i from winCount to 1 by -1
set newWin to make new Finder window
set target of newWin to item i of WIN_TARGETS
end repeat
end tell
tell application "System Events"
set i to 1
repeat with i from 1 to winCount
tell window i of application process "Finder"
# !! In the "System Events" context, windows do not
# !! have `bounds` properties, but separate `position` and
# !! `size` properties.
set position to item i of WIN_POSITIONS
set size to item i of WIN_SIZES
end tell
end repeat
end tell
After scouring the internet and much trial & error I have found this works well providing you allow the AppleScript Editor (or automator or your own app, whichever you are using) to control your computer under System Preferences > Security & Privacy > Privacy > Accessibility (OS X 10.9 Mavericks). The first time you run the script/app you'll be prompted to change the setting. After granting access, the second time you run the script/app the windows will be re-positioned.
do shell script "open -n /Applications/VLC.app /path/to/first/file" --edit path
do shell script "open -n /Applications/VLC.app /path/to/second/file" --edit path
delay 3 --wait 3s for VLC to open files, increase if necessary
tell application "System Events"
set pidList to the unix id of (every process whose name contains "VLC")
tell (first process whose unix id is item 1 of pidList)
set the position of the front window to {0, 22} --edit {x,y}
end tell
tell (first process whose unix id is item 2 of pidList)
set the position of the front window to {640, 22} --edit {x,y}
end tell
end tell
It's not as clever as mklement0's answer but, as someone who is new to AppleScript, I could understand what's going on.

Open multiple tabs in iTerm2 with specific directories

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

Using AppleScript to choose a file in Safari

I am trying to write some automation code (primarily in Ruby Selenium). At some point, a file chooser is opened in Safari so that the user can select a file for upload. Selenium cannot handle this, but I think AppleScript should be able to. I am new to AppleScript and haven't been able to find any boilerplate code of someone automating a file chooser dialog. I'm reading through the AppleScript docs, but any ideas would be most helpful.
Some more searching and I found a great answer here: Applescript file dialog with UI scripting
Here's what I ended up using:
on run argv
tell application "Safari"
activate
-- Usage check
set argc to count argv
if argc is not greater than 0 then
return "Usage: SafariFileChooser file_name [window_name]"
end if
-- The file we will choose to open
set file_name to item 1 of argv
-- Flip to the named window, if specified
if argc is equal to 2 then
set window_name to item 2 of argv
set flip_count to index of window window_name
repeat (flip_count - 1) times
activate
tell application "System Events" to keystroke "`" using command down
end repeat
end if
-- Interact with the dialog using System Events (thanks mcgrailm)
tell front window
activate
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke file_name
delay 1
keystroke return
delay 1
keystroke return
end tell
end tell
end tell
return 0
end run
Another option I just discovered is to specify the directory using the command-line:
do shell script "defaults write com.apple.Safari NSNavLastRootDirectory /path/to/directory"
This way you can do slightly less in UI scripting. Run this command before you open the file chooser, and it will put you into the directory specified. Include all the files you need in this directory, and you can just script command+a to select them all, and return.

Resources