Terminal extension - multiple issues

Hi,

I am finding the built-in Terminal extension failing regularly when using iTerm2 as the target.
Trying to fix it, first of all I wonder why the extension is called “Terminal” when the source is - confusingly - (still) named “Run Command”.
Then I realize that the version installed by the current version 4688 is way older then the github source - and I wonder, if there is an easy way to update all the built in extensions to the current ones. Am I overlooking something?

Looking into the downloaded source code it turns out that iterm2.applescript is still faulty, also in the current github version.

The following code will only (kind of) work if iTerm is already running with a window open and/or if iTerm2’s preferences are set to display a window on launch (I’m not even sure about this).

-- Get the "Use New Tab" option
	set use_new_tab to "{popclip option newtab}" is "1"
	
	-- Create a new tab if requested
	if use_new_tab then
		tell current window
			create tab with default profile
		end tell
		delay 0.5 -- Give the new tab time to initialize
	end if

Since (not exactly intelligently)

get current window

returns

missing value

if there is no open window instead of failing, the tell statement may be talking to missing value so the complete block - and everything below - may fail silently - and does so constantly for me.

Interestingly the line

get current tab of current window

errors if there is no open window, as I would have expected

get current window

to do.

Apart from that, the delay for initialization is hard coded

delay 0.5

and seems to be rather optimistic.

So I would like to propose the following more fault tolerant and flexible approach for the beginning of the code:

tell application id "com.googlecode.iterm2"
	-- Bring iTerm2 to the foreground
	activate
	
	-- Get the "Use New Tab" option
	set use_new_tab to "{popclip option newtab}" is "1"
	
	if get current window is missing value then
		create window with default profile
		
		-- open window exists, create a new tab if requested		
	else if use_new_tab then
		tell current window
			create tab with default profile
		end tell
	end if
	
	-- check if initalization is completed, by checking if changes in the
    -- window's title have died down
	set windowName to name of current window
	repeat
	        delay 0.3
		set tmpWindowName to name of current window
		if tmpWindowName is windowName then
			exit repeat
		else
			set windowName to tmpWindowName
		end if
	end repeat

This seems to work for me (at least).

Sadly, there is no easy way to fix the local extension without uninstalling the previous one, changing the identifier, reinstalling, etc. Sigh. Would have appreciated to be able to descend into the package, fix the Apple Script code and move on …

Thanks!

Thanks for your attention to the extension! Would you consider submitting a github pull request with the fix?


Regarding quick edits for yourself, this info may help since it lets you edit the code directly in the extension package and double click it to reload without changing any identifiers:

Turn off unsigned warning

If the unsigned extension warning gets annoying while you test your work, you
can turn it off. Run the following command at the Terminal, then Quit and
restart PopClip:

defaults write com.pilotmoon.popclip LoadUnsignedExtensions -bool YES

And if you are working on fixing an extension with the com.pilotmoon.
identifier prefix:

defaults write com.pilotmoon.popclip AllowUnsignedReservedPrefixes -bool YES

How? Feel free to just grab it and paste it in yours. I don’t claim any copyright and its dead simple boilerplate. Just has to work.
Just made a minor code edit regarding the delay value.

And thanks to your pointer to the developer docs. Much appreciated!

Yeah, that’s the whole thing to learn if you don’t know how to do it! I’ll just grab yours from here.