-2

I tried this

tell application "Terminal"
    set custom to open "/Users/jredfox/Desktop/h.terminal"
    do script "/Users/jredfox/Desktop/test.sh" in custom
    activate
end tell

this just says failed. I don't want to hard code window 1 because window 1 isn't always going to be the custom window I just opened.

My Goal is to open up the terminal with a custom profile then run a script inside of it. I am new to AppleScript and don't understand why this isn't working. I want to use the window that just got opened not window 1 which fails sometimes and doesn't support multiple windows

jredfox_
  • 19
  • 6

1 Answers1

3

One helpful habit when programming is to keep an eye on what actually happens after you add any unfamiliar code. The quick way to do this in AppleScript is with the result variable. Commands' return values go to result, but you can also explicitly set it for clarity. The value in result is shown at the bottom of the default window in Script Editor.

For example, I pasted in just this portion of your script, with a tweak:

tell application "Terminal"
    set result to open "/Users/jredfox/Desktop/h.terminal"
end tell

Execution took waaay too long, and the result was missing value. So right off the bat we know nothing else is going to work. But why?

I chose "Open Dictionary…" from the "File" menu, and chose Terminal. It seems Terminal uses the open command from the Standard Suite, which means it by default wants an AppleScript path, not a POSIX path.

set result to open "Macintosh HD:Users:jredfox:Desktop:h.terminal"

Well, it opened instantly now, but even that doesn't help as result isn't right. It seems the open command does not return a reference to the new window, as I believe you were expecting.

Experimenting with results a bit more, (and reading in the dictionary that there is a return value from do script); oh, and if you're willing to assume that h.terminal will always open successfully in less than one second, and will therefore be window 1; I rejiggered your script to the following:

tell application "Terminal"
    activate
    open "Macintosh HD:Users:jredfox:Desktop:h.terminal"
    delay 1
    set custom to (do script "/Users/jredfox/Desktop/test.sh" in window 1)
end tell
Joel Reid
  • 955
  • 1
  • 8
  • 14
  • uses window 1 which is the focused window not the executing window. I tested it with a java program and executed osascript. window 1 != executing window that called it. also your code when terminal is not running will provide two terminal windows due to an AppleScript bug – jredfox_ May 28 '22 at 02:30
  • @jredfox_ It's not an AppleScript bug, it's Terminal, which either has the behaviour defined in its activation policy or by how it responds to certain Apple events. AppleScript, however, is doing what it's supposed to. – CJK May 28 '22 at 05:09
  • it's a bug. if you can't open up a new script without a ui appearing by simply using AppleScript it's a bug. shouldn't open two windows when only one is suppose to be defined. – jredfox_ May 28 '22 at 06:51
  • It's not a bug unless you can prove it doesn't meet the requirements specification. Sure you can't use window 1 without further checks but opening a new window, and running a script in that same window is very easily doable. – user3579815 Jun 01 '22 at 02:39