Window/Document manipulation

I’ve spent the best part of that sunny Sunday we just had struggling with Windows/Document manipulation…

I’m trying to create a “scratch buffer” to test some AS code, in Script Debugger. I’ve tried the same trivial code targeting SE and it works and it must be trivial and I’m kind of stuck.

tell application "Script Debugger"
    set scratch_window to make new document
    set bounds of window 1 to {645, 0, 1280, 795}
    set name of window 1 to "Scratch Buffer"
end tell

My first attempt was about opening a “Scratch.scpt” script in its own window and changing all sorts of properties, but I got stuck in all kind of places and I had to quit…

Try this:

tell application "Script Debugger"
	set scratch_doc to make new document
	tell scratch_doc
		set bounds of window 1 to {645, 0, 1280, 795}
		--set name to "Scratch Buffer" -- not allowed - muse save document to change its name
	end tell
end tell

Remember, the document object represents the script, and has associate windows that let you view the script. Windows don’t have a name, the document does.

1 Like

Jean,

My solution was to create a saved file “My Scratch Pad.scpt”, and configure the window it is in as I want it to be. This also has the advantage of saving my last “scratching”. :wink:

For details, see:

I also have a couple of Keyboard Maestro Macros that will auto-configure the window layout when SD is launched, and to ensure new files opened in SD are opened in the main window (not the Scratch Pad).

If you, or anyone, is interested in these KM Macros, please let me know and I’ll post. They are still in the final beta stage, but as soon as I have time-tested them for a bit, I’ll publish on the KM forum, and here.

1 Like

Ok, I was trying to find all sorts of ways to have the name set…

But an “Untitled” document is not saved yet and still it has a name. Is there a technical reason why you chose to not allow renaming before saving ? Script Editor/TextEdit/BBEdit and a bunch of others allow for that. Which is not to say that SD should, but that would help.

Jim,

It’s actually your post that got me thinking. I’m working on a 13" so your layout was not possible. But I tried a number of other things and I was stuck with the naming part…

No problem.

  1. Use whatever layout works for you.
  • Close all other windows except for the Scratch Pad.
  • Position it where you want it.
  • Save.
  • Quit

Now when you launch SD6, the Scratch Pad should appear where you last left it. IAC, I have KM macros that trigger when SD6 is launched, and position/setup everything as I want it. I’m sure the same could be done in script, but it was very easy for me to use KM.

1 Like
tell application "Script Debugger"
	activate
	if "Scratch.scpt" is in name of documents then
		set scratch_document to document named "Scratch.scpt"
	else
		set scratch_document to open "/path/to/Scratch.scpt"
		tell scratch_document
			set bounds of its window to {645, 0, 1280, 795}
		end tell
	end if
	tell scratch_document
		set index of its window to 1
	end tell
end tell

When I run the above code in SD:

  1. if Scratch.scpt is not opened, it properly comes to the front and gets the focus
  2. if Scratch.scpt is already opened and hidden by other windows, it comes to the front but does not get the focus.

What am I doing wrong?

You’re assuming that documents have a window property, rather than window elements. I’m surprised that line doesn’t error. However, even using window 1 exhibits the same behavior.

Is there some reason you’re not using move scratch_document to front?

Yes, it never occurred to me to use this syntax :slight_smile:

Hey Shane,

Because it doesn’t work properly in SD6 or SD5?

It takes forever to run the first time, and it creates a second document with the same name on the second run.

Jean-Christophe — there has been a terrible and longstanding bug in the macOS since Mountain Lion (IIRC) that prevents a window set to index 1 from gaining focus.

The least impactful workaround is to use System Events to complete the window-focus.

------------------------------------------------------------------------------
# Plain Call to Script Debugger
------------------------------------------------------------------------------

tell application "Script Debugger"
   set doc to document "Untitled 6"
   set docWin to item 1 of (get doc's windows)
   set index of docWin to 1
end tell

tell application "System Events"
   tell application process "Script Debugger"
      tell window 1
         perform action "AXRaise"
      end tell
   end tell
end tell
------------------------------------------------------------------------------
# Differentiate Script Debugger 6
------------------------------------------------------------------------------

tell application id "com.latenightsw.ScriptDebugger6"
   set doc to document "Untitled"
   set docWin to item 1 of (get doc's windows)
   set index of docWin to 1
end tell

tell application "System Events"
   tell (first process whose bundle identifier is "com.latenightsw.ScriptDebugger6")
      tell window 1
         perform action "AXRaise"
      end tell
   end tell
end tell

------------------------------------------------------------------------------

-Chris

1 Like

Indeed, I tried “move to front” and there were weird side effects as you wrote. Also, I did not understand @ShaneStanley’s distinction between elements and properties so I guess that means I’m back to the Language Reference :slight_smile: But really, all that ought to be a bit more simple…

I’ll try your code later today Chris.

@ccstone Your code works fine. But I had trouble with it at first because it looks like the “move to front” had created a window less document and my loop was not handling that properly (I had “Scratch.scpt” in names of documents, but I could not access it’s window). Since I don’t know how to kill buffers in SD, I killed SD and then everything worked perfectly.

Thanks for this script/solution. I just ran into it and thought I was doing something wrong, or maybe a SD bug.

I can use this as part of my toggle tabs system.

1 Like