Copy path with Atom code editor

I try to copy the path of the document in the code editor Atom. I tried many things and it does not work. For instance:

tell application “Atom”
– set filePath1 to path of front document
– get path of document frontmost
– get path of document 1
end tell

In the Dictionary there is the option “document” and inside “path”. But it seems I do not know how to apply it

It seems that there is a bug somewhere.
After creating a document saved as “blahblah” I tested different syntax. The only one returning something is :

tell application "Atom"
	properties of window 1
end tell

The events log is :

{document:missing value, closeable:true, zoomed:false, class:window, index:1, visible:true, name:“blahblah — ~/Documents”, modal:false, miniaturizable:true, titled:true, id:1276, miniaturized:false, floating:false, resizable:true, bounds:{923, 407, 1947, 1464}, zoomable:true}

You read well,the property document is missing value.

I ran also:

tell application "Atom"
	properties of windows
end tell

This time the events log is :

{{document:missing value, closeable:true, zoomed:false, class:window, index:1, visible:true, name:“blahblah — ~/Documents”, modal:false, miniaturizable:true, titled:true, id:1276, miniaturized:false, floating:false, resizable:true, bounds:{1541, 739, 2565, 1796}, zoomable:true}, {document:missing value, closeable:true, zoomed:false, class:window, index:2, visible:false, name:“Atom”, modal:false, miniaturizable:true, titled:true, id:-1, miniaturized:false, floating:false, resizable:true, bounds:{560, 218, 1360, 818}, zoomable:true}}

Not too surprised to see that the 2nd window is described with visible:false because I really don’t see it but at least I see that for both windows, document is missing value.

I guess that you will have to ask the authors about this feature.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 30 septembre 2019 21:14:05

It seems, this application is not scriptable. As I see in Script Debugger, it has not Cocoa objects . The only Cocoa object is application itself. One solution is this:


tell application "Atom" to activate
tell application "System Events" to tell process "Atom"
	tell window 1 to set thePath to value of attribute "AXDocument"
end tell

NOTE: the code in the Atom editor window must be saved file to get correct results. Otherwise, you will get the path to the last directory in which the Atom program worked, or missing value.

To get many other properties and actions of window 1:


tell application "Atom" to activate
tell application "System Events" to tell process "Atom"
	tell window 1 to set atomWindow to it
end tell
tell me to activate
return atomWindow

I use Script Debugger. It finds all you need.

In Script Editor maybe handful this:


tell application "Atom" to activate
tell application "System Events" to tell process "Atom"
	tell window 1 to set atomWindow to it
	--set atomWindowActions to actions of window 1
	--set atomWindowAttributes to attributes of window 1
	--set atomWindowButtons to buttons of window 1
	--set atomWindowImages to images of window 1 -- this appears only when you have opened some saved file
	--set atomWindowUIElements to UI Elements of window 1
	--set atomWindowEntireContents to entire contents of window 1
	--set atomWindowPosition to position of window 1
	--set atomWindowProperties to properties of window 1
end tell
tell me to activate
atomWindow
--atomWindowActions
--atomWindowAttributes
--atomWindowButtons
--atomWindowImages -- is one nice way to check if is opened some saved file
--atomWindowUIElements
--atomWindowEntireContents
--atomWindowPosition
--atomWindowProperties

Uncomment what you need and go on!

I apologizes but, as I wrote, none of the grabbed properties return the path of the document open at front.

If I read well, the application disagree with your statement about scriptability.

set thePath to (path to applications folder as text) & "Atom.app:Contents:Info.plist"
tell application "System Events" to tell property list file thePath to tell contents
	value of property list item "NSAppleScriptEnabled" --> "YES"
end tell

Running the script :

set thePath to (path to documents folder as text) & "blahblah"
tell application "Atom"
	open file thePath
	properties of windows
end tell

correctly opens the described file and return:

{{document:missing value, closeable:true, zoomed:false, class:window, index:1, visible:true, name:“blahblah — ~/Documents”, modal:false, miniaturizable:true, titled:true, id:408, miniaturized:false, floating:false, resizable:true, bounds:{461, 694, 1485, 1751}, zoomable:true}, {document:missing value, closeable:true, zoomed:false, class:window, index:2, visible:false, name:“Atom”, modal:false, miniaturizable:true, titled:true, id:-1, miniaturized:false, floating:false, resizable:true, bounds:{560, 240, 1360, 840}, zoomable:true}}

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 1 octobre 2019 14:46:05

Its Info.plist file says it is, but it doesn’t actually have a scripting dictionary. So in practical terms it is unscriptable except via GUI scripting.

Thank you very much!! All the answers are very useful.

I suppose there is no easy way to get the path in all the code editors: Brackets, Sublime Text, BBEdit… I suppose I have to find the way for each one. Is that right?

No, no need find the way for each one. You get path to document from the same script I provided. Independently to code editor is currently used in Atom window. The only you need is the code currently edited in the Atom window to be saved file on the disk before running the script.

Hi KniazidisR.

Your script returns URLs, not paths. Some extra work is needed:

use AppleScript version "2.5" -- Mac OS 10.11 (El Capitan) or later.
use framework "Foundation"

tell application "BBEdit" to activate
tell application "System Events" to tell application process "BBEdit"
	tell window 1 to set theURL to value of attribute "AXDocument"
end tell

set theNSURL to current application's class "NSURL"'s URLWithString:(theURL)
-- Then either:
set thePosixPath to (theNSURL's |path|()) as text
-- Or:
set theHFSPath to theNSURL as «class furl» as text

Thank your for your answer.
I have and use BBEdit and the script gives me that error:
class “NSURL” doesn’t understand the “URLWithString_” message

I think I did not explain well. People can use Atom, or Sublime Text or Brackets… For instance, I open an HTML page with Adobe Brackets and save it. If I use your script it gives me an error “missing value”

You should use framework “Foundation” as it is AppleScriptObjC code. I tested. Works fine. And, if you work in Atom.app with BBEdit code (not in BBEdit.app), then you should tell to Atom application. No need open BBEdit application. Atom app works with plists and HTML without BBEdit involved.

Tell to Adobe Brackets.app (not tell application process “Atom”, but tell application process “Adobe Brackets”) when you use it, and not “Atom”. You should always tell to application, whose window is frontmost, and not to other. This is GUI scripting. You tell to environment, whose window is accessible current time.

Of curse I tell to Brackets
I open a document with Brackets. I use this script in the Script Editor and it gives: missing value.

tell application “Brackets” to activate
tell application “System Events” to tell process “Brackets”
tell window 1 to set thePath to value of attribute “AXDocument”
end tell

Unfortunately, there is a flaw in application “Brackets” - this attribute is not served by the application. But I found a workaround. I don’t know, maybe there is something better, but this works:


tell application "Brackets" to activate
tell application "System Events" to tell process "Brackets"
	tell menu item 7 of menu 1 of menu bar item "Window" of menu bar 1
		set posixPath to value of attribute "AXTitle"
	end tell
end tell

set x to the length of " (Getting Started) — Brackets"
set posixPath to characters 1 thru -(x + 1) of posixPath as string

It does not work always. But it helps me a lot as it guides me with what I have to learn and try. Thank you very much!