AppleScript how to get the document of current tab

I'm trying to get the current source document using applescript. I need to modify the text selection. Getting the first, last or front source document doesn't work all the time. You ask for front document and it may be correct. Then you change docuemts and ask again and you still get the last one. It also needs to work if Tab windows are shown. I don't see any object that contains the current editing document. I also need to know if the document has the current focus, don't want to do a copy if the edit window isn't the focus.

AS is typically discussed on Apple's public forums in Mac OS X Technologies


Xcode/dev scripting is a different animal 😉, but if that's what you're asking about, which version are you working with?


Is this a duplicate question? AppleScript how to get the document in the asistent editor

I just sussed out the following:

Code Block Applescript
tell application "Xcode"
set t to name of front window
set o to offset of " — " in t
if o > 0 then set t to text 1 thru (o - 1) of t
set d to source document named t
end tell

It works in Xcode 11 & 12, with stand-alone file windows, files opened in tabs in a workspace document, and assistent windows.

Here's what works for me in Xcode 14:

tell application "Xcode"
	set x to path of last item of source documents
	display dialog x
end tell
AppleScript how to get the document of current tab
 
 
Q