Get Document Path Using GUI Scripting???

I am trying to get the document path of the frontmost window in Adobe GoLive. GoLive has a very limited scripting dictionary, so I’m using System Events 1.2 to grab the information from GoLive. I can get to the front window information, but I can’t get to any of the document information. I’ve used the UI Element Inspector and it shows the document path–here’s the info it gives:

<AXApplication: “GoLive”>
 <AXWindow: “untitled.html”>

Attributes:
   AXRole:  “AXWindow”
   AXRoleDescription:  “window”
   AXChildren:  “<array of size 5>”
   AXParent:  “<AXApplication: “GoLive”>”
   AXTitle:  “untitled.html”
   AXSize (W):  “w=634 h=427”
   AXPosition (W):  “x=23 y=74”
   AXMain (W):  “1”
   AXFocused (W):  “1”
   AXMinimized (W):  “0”
   AXCloseButton:  “<AXButton>”
   AXZoomButton:  “<AXButton>”
   AXMinimizeButton:  “<AXButton>”
   AXTitleUIElement:  “<AXStaticText>”
   AXGrowArea:  “<AXGrowArea>”
   AXDocument:  “file://localhost/Volumes/Amonet/Desktop/untitled.html”
   AXProxy:  “<AXImage>”

I have tried every possible way of getting the AXDocument information and have had no luck. Does anyone have any ideas?? Any help would be great.

I’m not sure if this will help or not, try…

tell application "Application Name" to get properties of document frontmost

Where “Application Name” would be the name of the application that owns the document you are getting the info of. To get just the path of the frontmost document, you could try…

tell application "Application Name" to get path of document frontmost

hth.

Unfortunately, Go Live’s dictionary is so limited, it can’t even handle that. :frowning:

The authors of the book Real World GoLive 6 say this:

That’s why I was going the System Events route. If it can be done, it most likely can only be done through System Events.

For the record, this works in Preview, which is also Applescript-challenged:

tell application "System Events"
	tell process "Preview"
		set thefile to value of attribute "AXDocument" of window 1
	end tell
end tell

This will result in an HTML-encoded string that can be converted to a file path (most of the time) using the following:

set text item delimiters to "localhost"
set thefile to item 2 of text items of thefile
set text item delimiters to ""

set thefile to decode_text(thefile)
set thefile to POSIX file thefile

where the subroutine decode_text can be obtained from Apple at http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.10.htm

I use this code to open PDFs that I am viewing in Preview using a PDF editor such as Acrobat.

As long as the pattern of the returned string is stable, scb’s script could be simplified thus:

tell application "System Events"
	tell process "Preview"
		set thefile to value of attribute "AXDocument" of window 1
	end tell
end tell
set fixFile to characters 17 thru -1 of thefile
set thefile to fixFile as string
set thefile to POSIX file thefile
thefile

casdvm

I can’t leave this alone. What if the returned string was not the same length every time? Using the offset of [/Users] should also do the trick, and be more versatile:

tell application "System Events"
	tell process "Preview"
		set thefile to value of attribute "AXDocument" of window 1
	end tell
end tell

set o_set to offset of "/Users" in thefile
set fixFile to characters o_set thru -1 of thefile
set thefile to fixFile as string
set thefile to POSIX file thefile
thefile

casdvm, to fuel your obsession, consider the situation where high-ASCII characters, such as bullets, are in the pathname. These are represented within a URL using UTF-8 encoding, so that a file path

is represented with the URL

The script above will not work in this case.

When I posted a question about this, a kind reader replied with a very effective but (to me) unintelligible solution. See http://bbs.applescript.net/viewtopic.php?pid=48201.

Cool; I see what you mean. That is a way funky script, and it fits right in with other stuff I have read from hhas. She or he is one talented scripter.

Thanks for the update.

casdvm

The link to the funky script by hhas is out of date. Here’s an updated one: http://macscripter.net/viewtopic.php?id=14861 .

Assuming you have some file opened by Preview.app, 2 effective methods to get document HFS path using GUI scripting:

Perl - method:
 

tell application "System Events" to tell process "Preview"
	set localURL to value of attribute "AXDocument" of window 1
end tell

localURLtoHFS(localURL)

on localURLtoHFS(theText)
	set posixPath to do shell script "perl -e 'use URI::Escape; print uri_unescape(\"" & quoted form of theText & "\")';"
	posixPath as «class furl» as text
	-- or:
	-- posixPath as POSIX file as text
end localURLtoHFS

 
AsObjC - method:
 

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "System Events" to tell process "Preview"
	set localURL to value of attribute "AXDocument" of window 1
end tell
localURLtoHFS(localURL)

on localURLtoHFS(sourceText)
	set sourceString to current application's NSString's stringWithString:(text 9 thru -1 of sourceText)
	set adjustedString to sourceString's stringByRemovingPercentEncoding()
	return (adjustedString as «class furl» as text)
	-- or:
	-- return (adjustedString as POSIX file as text)
end localURLtoHFS

 

Then, the AsObjC-method for obtaining the HFS path of a document is also simplified.
 

use framework "Foundation"
use scripting additions

tell application "System Events" to tell process "Preview" to ¬
	set localURL to value of attribute "AXDocument" of window 1
localURLtoHFS(localURL)

on localURLtoHFS(localURL)
	(my (|NSURL|'s URLWithString:localURL)) as «class furl» as text
end localURLtoHFS