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:
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
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
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
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
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.
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