For my subscribers, here’s a Fish function for sending files from the current iTerm pane to another pane in the same tab.
When working in Forklift or Path Finder, I love the ability to send files from one pane to the other with a keyboard shortcut when browsing in split pane mode. I wanted to replicate that in iTerm. This is written for Fish, but the AppleScript it uses can easily be repurposed for other shells.
[paywall]
Here’s the contents of the function:
“`fish
function send -d “Send to other iTerm pane”
# Parse options:
# -m / –mv move instead of copy
# -h / –help show this help
argparse m/mv h/help – $argv
or return
if set -q _flag_h
echo "send - Send to other iTerm pane"
echo
echo "Usage: send [OPTIONS] PATH..."
echo
echo "Options:"
echo " -m, --mv Move PATHs instead of copying (default is cp -r)."
echo " -h, --help Show this help message."
return 0
end
set -l do_move 0
if set -q _flag_m
set do_move 1
end
set -l target_dir (begin
echo 'tell application "iTerm"'
echo ' set theTab to current tab of current window'
echo ' set sessionList to sessions of theTab'
echo ' set pwdList to {}'
echo ' set currSession to current session of theTab'
echo ' repeat with aSession in sessionList'
echo ' tell aSession'
echo ' if id of aSession is not id of currSession then'
echo ' write text "pwd"'
echo ' delay 0.2 -- adjust if needed for slow shells'
echo ' set sessionText to contents'
echo ' set sessionLines to paragraphs of sessionText'
echo ' set pwdOutput to ""'
echo ' repeat with i from (count of sessionLines) to 2 by -1'
echo ' if (item i of sessionLines) is not "" and (item (i - 1) of sessionLines) contains "pwd" then'
echo ' set pwdOutput to item i of sessionLines'
echo ' exit repeat'
echo ' end if'
echo ' end repeat'
echo ' set end of pwdList to pwdOutput'
echo ' end if'
echo ' end tell'
echo ' end repeat'
echo ' return item 1 of pwdList'
echo 'end tell'
end | osascript -)
# Normalize directory path once
set target_dir (echo $target_dir | string trim)
# Now do whatever “send the $argv paths to that directory” means
for p in $argv
if test $do_move -eq 1
mv $p $target_dir/
warn "Moved $p to $target_dir"
else
cp -r $p $target_dir/
warn "Copied $p to $target_dir"
end
end end ```
This script uses my warn function, which can just be replaced with echo if you want, or you can install warn.
Here’s just the AppleScript portion if you want to incorporate it in
other scripts:
applescript
tell application "iTerm"
set theTab to current tab of current window
set sessionList to sessions of theTab
set pwdList to {}
set currSession to current session of theTab
repeat with aSession in sessionList
tell aSession
if id of aSession is not id of currSession then
write text "pwd"
delay 0.2 -- adjust if needed for slow shells
set sessionText to contents
set sessionLines to paragraphs of sessionText
set pwdOutput to ""
repeat with i from (count of sessionLines) to 2 by -1
if (item i of sessionLines) is not "" and (item (i - 1) of sessionLines) contains "pwd" then
set pwdOutput to item i of sessionLines
exit repeat
end if
end repeat
set end of pwdList to pwdOutput
end if
end tell
end repeat
return item 1 of pwdList
end tell
Not to spoil the “exclusivity” of this post, but the send function is available in my Fish Files repo, where you’ll find a bunch of other useful functions.
[/paywall]

Leave a Reply