Skip to content

Instantly share code, notes, and snippets.

@gnachman
Last active April 16, 2022 23:42
Show Gist options
  • Save gnachman/4cbe6743baa7fe07536b to your computer and use it in GitHub Desktop.
Save gnachman/4cbe6743baa7fe07536b to your computer and use it in GitHub Desktop.
Script to use Alfred with nightly build of iTerm2
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as string
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
on alfred_script(q)
-- Write the command to run to a file. This is done because Applescript quoting is impossible to get right, esp. for backslashes.
set tmp_dir to path to temporary items as string from user domain
set applescript_alfred_file to tmp_dir & "alfredscript"
set alfred_file to POSIX path of applescript_alfred_file
write_to_file(q & return, applescript_alfred_file, false)
-- Create this file, which prevents iTerm2 from restoring a saved window arrangement.
do shell script "touch ~/Library/Application' Support/iTerm/quiet'"
-- Test cases:
-- 1. iTerm2 running, has windows open. Should open a new window for Alfred command.
-- 2. iTerm2 running, no windows open. Should open a new window for Alfred command.
-- 3. iTerm2 not running, set to restores arrangement. Should not restore arrangement but open a new window for the Afred command.
-- 4. iTerm2 not running. No windows to restore. Should open a single window for the Alfred command.
-- 5. iTerm2 not running. Has windows to restore. Restores windows and then opens a new window for the Alfred command.
-- Compose a script. This is necessary because compiling in a 'tell application' command causes the app to be launched, which would happen prior to the creation of the quiet file.
set theScript to "tell application \"iTerm2.app\"
if (exists current window) then
tell current window to create tab with default profile
tell current session of current window
write contents of file \"" & alfred_file & "\"
end tell
else
create window with default profile
tell current session of current window
write contents of file \"" & alfred_file & "\"
end tell
end if
activate
end tell"
-- Invoke the script.
run script theScript
-- Clean up
-- do shell script "rm -f ~/Library/Application' Support/iTerm/quiet' /tmp/alfredscript"
end alfred_script
@jamesstout
Copy link

This no longer works with Build 3.1.beta.5. Causes a seg fault. It doesn't start so I can't get any debug logging.

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [0]

VM Regions Near 0:
--> 
    __TEXT                 0000000109779000-0000000109ac0000 [ 3356K] r-x/rwx SM=COW  /Applications/iTerm.app/Contents/MacOS/iTerm2

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   com.apple.Foundation          	0x00007fffbaa5a116 -[NSScriptCommand _sendToRemainingReceivers] + 829
1   com.apple.Foundation          	0x00007fffbaa5a3d4 -[NSScriptCommand executeCommand] + 512
2   com.apple.Foundation          	0x00007fffbaa75194 -[NSScriptingAppleEventHandler handleCommandEvent:withReplyEvent:] + 195
3   com.apple.Foundation          	0x00007fffba8d6d8d -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 290
4   com.apple.Foundation          	0x00007fffba8d6c07 _NSAppleEventManagerGenericHandler + 102
5   com.apple.AE                  	0x00007fffb9cdaf26 aeDispatchAppleEvent(AEDesc const*, AEDesc*, unsigned int, unsigned char*) + 544
6   com.apple.AE                  	0x00007fffb9cdac9d dispatchEventAndSendReply(AEDesc const*, AEDesc*) + 39
7   com.apple.AE                  	0x00007fffb9cdaba9 aeProcessAppleEvent + 312
8   com.apple.HIToolbox           	0x00007fffb83e7ddf AEProcessAppleEvent + 55
9   com.apple.AppKit              	0x00007fffb696fd1d _DPSNextEvent + 1833
10  com.apple.AppKit              	0x00007fffb70eb7ee -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2796
11  com.apple.AppKit              	0x00007fffb69643db -[NSApplication run] + 926
12  com.apple.AppKit              	0x00007fffb692ee0e NSApplicationMain + 1237
13  com.googlecode.iterm2         	0x000000010977b0ce main + 110
14  libdyld.dylib                 	0x00007fffcea52235 start + 1

Running the AppleScript in ScriptEditor you get this error:

error "iTerm got an error: Connection is invalid." number -609

And the create tab part of L39 is highlighted.

tell current window to create tab with default profile

I saw there were some changes in the sdef file, but I'm not too familiar with what the keys mean. Any ideas? I'll keep playing in Script Editor for a while....

thanks

@yrocq
Copy link

yrocq commented Apr 20, 2020

Hi George,

Thank you for this script. I had to make a few tweaks to make it work with the current version of iTerm. You can get it here :

https://gist.github.com/yrocq/4c5d6e3b10e38ed31f4fbbfbaa7e15e9

Have a good day.

@yigitefe
Copy link

yigitefe commented Apr 16, 2022

Hi everyone,

I had a problem finding a working script, and ended up in this discussion. I had to change yrocq's script a little bit to get it working for me.

https://gist.github.com/yigitefe/abbb73f5079f8ab72e8486437c6536c1

I hope someone finds this useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment