How to get filename without extension from Omnigraffle? - macos

I'm trying to get the filename without the extension of the current document file in Omnigraffle Professional 5.
tell application "OmniGraffle Professional 5"
set _document to front document
set _path to path of _document
-- Get filename without extension
tell application "Finder"
set {_filename, _extension, _ishidden} to the
{displayed_name, name_extension, extension_hidden} of the _path
end tell
end tell
This gives me the following error: error "Can’t get displayed_name of \"/Users/ca/Downloads/Feb 8.graffle\"." number -1728 from displayed_name of "/Users/ca/Downloads/Feb 8.graffle".
I found some related questions and pages, but I'm a bit lost and really can't understand why it does not work.
Applescript: Get filenames in folder without extension
Applescript Help...Just the File Name
Thanks for your help!

You'd need to change it to the following:
tell application "OmniGraffle Professional 5"
set _document to front document
set _path to path of _document
-- Get filename without extension
tell application "Finder"
set {_filename, _extension, _ishidden} to the ¬
{displayed name, name extension, extension hidden} ¬
of ((the _path as POSIX file) as alias)
end tell
if (_extension ≠ missing value) then
set baseName to text 1 thru -((length of _extension) + 2) of _filename
end if
end tell
"path of front document" returns the POSIX path of a file, which is just a plain string. To be able to get information on an item the Finder will want an alias reference to the file in question. When you pass a plain string it gets an error because a plain string won't have those properties. To get an alias, you need to coerce the plain path first to a POSIX file and then coerce the POSIX file to an alias.
Unless you have defined these variables elsewhere, you need to remove the underscores in {displayed_name, name_extension, extension_hidden}. When you look at the "compiled" code with those underscores left in, it looks like in the following image:
So, the AppleScript is interpreting displayed_name to be a variable, not a property. Now, that's fine if you've defined those variables elsewhere, such as at the top of your script in properties. But if not, you need to remove the underscores, as the property names of Finder items don't have underscores in them. When you remove the underscores, the coloring appears correct (properties are purple with the variables being green).
Note that that still won't give you the filename without an extension. To get that, you'd need to do something like I did in the added line using text n thru m
if (_extension ≠ missing value) then
set baseName to text 1 thru -((length of _extension) + 2) of _filename
end if

First, you need to use the correct labels for the properties of whatever application you are targeting - these can be found in the application scripting dictionary. The next problem is that the Finder doesn't know anything about POSIX paths, which is apparently what OmniGraffle is returning for the document path, so you need to coerce the path into something that the Finder does know about, such as an alias.
tell application "Finder"
set {_filename, _extension, _ishidden} to the {displayed name, name extension, extension hidden} of (_path as POSIX file as alias)
end tell

Related

Getting last encoded file information from Apple Compressor

So I'm a novice with AppleScript but learning a lot. I'm trying to create an AppleScript to let me know when Apple Compressor is finished encoding and notify me if it was successful or not. I have the part down about telling when compressor is done and the notification but having difficulty getting the file name and status (complete/fail/cancelled) from Compressor's history log.
Originally tried with UI scripting, but Compressor's window hierarchy list changes with every new encode. So now onto pulling it from the history log, which is in the user folder "Library/Application Support/" folder like this:
And then, the file itself contains the status (whether it was successful or failed) near the bottom of the file like this:
So getting confused about how to A) find the last file and its name, and B) get the value of the last "ElementStatusState" in the document.
This is where I started but get errors when trying to resolve the path to the folder:
tell application "Finder"
set latestFile to (last item of (sort (get files in folder "~/Library/Application Support/Compressor/History/V4" of application "System Events") by name)) as text
set fileName to latestFile's name
end tell
This code throws an error about not being able to make it into the expected file type but also, I can see that It's grabbing a file that's not the last one modified.
In the end I want 2 variables that are 1) theFileName = ie name of encoded movie(s), and 2) theStatus = ie number 4,5, or 6)
Any ideas out there?
You'll want to do something like the script below. This script finds the most recently modified file, then drills down through the plist file to find the various status numbers, and the names of the records they are logged in. I'm assuming that every file Compressor generates has the same structure; if not, you may find you have to alter the script. But this should give you an idea how to do that.
Names and status values are stored in the nameList and valueList variables, in order of depth.
set filePath to POSIX file "~/Library/Application Support/Compressor/History/V4"
-- sort the file list by modification date, then get the last item.
tell application "Finder"
set mostRecentFile to last item of (sort files of folder filePath by modification date) as alias
end tell
set mostRecentFilePath to POSIX path of mostRecentFile
set nameList to {}
set valueList to {}
tell application "System Events"
set plistFile to property list file mostRecentFilePath
tell plistFile
tell (first property list item whose kind is record)
copy value of property list item "ElementInfoName" to end of nameList
copy value of property list item "ElementStatusState" to end of valueList
tell (first property list item whose kind is list)
tell (first property list item whose kind is record)
copy value of property list item "ElementInfoName" to end of nameList
copy value of property list item "ElementStatusState" to end of valueList
tell (first property list item whose kind is list)
tell (first property list item whose kind is record)
copy value of property list item "ElementInfoName" to end of nameList
copy value of property list item "ElementStatusState" to end of valueList
end tell
end tell
end tell
end tell
end tell
end tell
end tell

How to create a new document in TexShop?

I want to create a new document in TexShop at a specific directory. The documentation says for the command make it has the option at location specifier. I have not been able to see anywhere in the documentation what actually qualifies as a "location specifier". I have tried alias [some path] but only gotten the results can't make into type location specifier or File alias X wasn't found (if it is a new filename I append to the end of the path). I have also tried it with just the path as text without alias, with the same results. What other kind of location specifier could there be? Or is there some strange rule that you have to create the file in the Finder first before calling TexShop's make new document command?
tell application "TeXShop"
make new document at alias "Users:jukhamil:Desktop:apples.tex"
end tell
Error message:
File alias Users:jukhamil:Desktop:apples.tex wasn’t found.
the at location specifier does not refer to the file system but to the list of the documents of the application like in
make new document at the beginning of documents
make new document at the end of documents
Instead you should set the path property and save eventually.
tell application "TeXShop"
log (count of documents) -- n
set p to (POSIX path of (path to desktop folder)) & "foo.tex"
set the_document to make new document with properties {path:p}
tell the_document to save
log (count of documents) -- n+2 whereas n+1 was expected
end tell
However, you should notice that until version 4.27 at least
1) TeXShop creates one window but 2 documents, meaning that iterating over the documents with AppleScript will loop twice as you can test by running
tell application "TeXShop"
set ds to documents
repeat with d in ds
log name of d as text
end repeat
end tell
2) TeXShop does not use the location specifier properly

How to Specify filepath but not name InDesign applescript

I am new to applescript.
I am trying to create an Automator script app that opens a batch existing files in InDesign, finds and changes text within the file. ( I thought this would be the complicated bit but it's not it was easy)
What I'm struggling with is then saving these files in another location but using the original filenames as I need to keep the original files.
I have a script to specify a path and a filename but I need to specify the path only and use the existing filename. Is this possible?
The code I tried was this:
tell application "Adobe InDesign CS5.5"
save document 1 to "users:xxx:Desktop:"
close document 1
end tell
It doesn't seem to work for the reason that I'm not specifying a filename BUT I DON'T WANT TO! Is there a way of calling up the original filename?
I'm assuming there must be a way of doing this, as I can't see the point of a script that is specific to one particular file.
My next step is to then rename the files by replacing the last bit of the filename eg:
xxx_xxx_M6.indd to xxx_xxx_M7.indd
I know how to do this in another script but if it can be done in the above section that would be great.
If you want to use the original file name when saving, you can just pull it from the file's properties and combine it with the path you want to save to, like so:
set origName to the name of document 1 as string
save document 1 to ("your:path:here:" & origName)
EDIT: If you already have your own routine for replacing the suffix, you can just perform those operations on origName before you pass it to the save command. I'll leave my look at suffix replacement below just in case it's helpful to anyone.
As for the second part of your question, about replacing a suffix, it depends on what exactly you want to do. From your example I’m guessing you want to increment a number, which you could do with the following code:
set thePoint to the offset of "." in origName
set firstPart to (characters 1 through (thePoint - 1) of origName) as string
set fpLength to the length of firstPart
set newSuffix to ((the last character of firstPart) as number) + 1
set newName to (characters 1 through (fpLength - 1) of firstPart) & newSuffix ¬
& ".indd" as string
This takes separates the file’s name from its extension, creates a new suffix by incrementing the last character (coerced to a number) of that name by 1, and then combines the lot to form a full file name that you can then use in the save command.
The key is breaking apart the original file name, then performing operations on the parts.
Now, this currently has a few limitations: any suffix other than a single digit makes things more complicated (though not impossible), and assumes that anyone running the script has “Show all filename extensions” enabled in the Finder’s preferences (this can be worked around, though).
Wrapping everything up gives us this:
tell application "Adobe InDesign CS5.5"
set origName to the name of document 1 as string
set thePoint to the offset of "." in origName
set firstPart to (characters 1 through (thePoint - 1) of origName) as string
set fpLength to the length of firstPart
set newSuffix to ((the last character of firstPart) as number) + 1
set newName to (characters 1 through (fpLength - 1) of firstPart) ¬
& newSuffix & ".indd" as string
save document 1 to ("your:path:here:" & newName)
end tell
If you could provide some more information about the suffixes you’d intend to use I’d be happy to update my answer.
InDesign documents have 3 properties that could interests you:
name : "xxx_xxx_M6.indd"
file path : file "Macintosh HD:sourceFolder:"
full name : file "Macintosh HD:sourceFolder:xxx_xxx_M6.indd"
So, to save (& close) the open file on the desktop, with same name, you could do this :
tell application "Adobe InDesign CS5.5"
save document 1 to "users:xxx:Desktop:" & name of document 1
close document 1
end tell

Get unrecognized file extension in applescript

I was wondering how to return just the file extension from a string. I've tried the 'set variable to name extension of...' detailed in this question, but that only seems to work for recognized extensions. The idea is to sort files with the extension '.meta' into their own collection.
What I have now looks like
tell application "Finder'
set everyName to name of every item in entire contents of this_folder
end tell
set metaFiles to {}
repeat with n from 1 to count of everyName
set currentName to item n of everyName
set currentExt to last word of currentName --this assignment fails
if currentExt is "meta" then
set end of metaFiles to currentExt
end if
end repeat
I'm brand new to applescript so I appreciate any and all help/direction. Thanks!
Edit: Hacky Solution
I solved this by using the split function described here to break up the filename after every period. I grabbed the last string, made sure it wasn't the same as the first string in case there were no period characters, and then stored the corresponding filename.
The name includes the file extension, whether the Finder recognizes it or not. So just sort on the name like this...
tell application "Finder"
set metaFiles to (every item in entire contents of this_folder whose name ends with "meta") as alias list
end tell
If you aren't getting a name extension, make sure there actually is one and that you aren't looking at the end of the name. If you are going to be moving files around, you will also need to get the path, and not just the name. I don't think that making a list of your extensions is what you are going for, either - several different characters are used for word boundaries, but a period isn't one of them.
Why not just ask Finder for your file items?
tell application "Finder"
set metaFiles to (every item in entire contents of this_folder whose name extension is "meta") as alias list
end tell

Store returned reference of tell Finder action in Applescript

I am building a Applescript droplet to automate some stuff. I have the following line:
tell application "Finder" to duplicate dropped
Dropped being a reference to the file that was dropped on the droplet. The documentation says that this returns a reference to the duplicated object.
I want to set myVariable to the reference that is returned but I can't find in any of the documentation how to actually do that!
if it's a droplet, be aware that the parameter is a list of aliases (you can drag more than one file!), and that if you duplicate a single finder item you will get a finder item, whereas if you duplicate more than one finder item, you will get a list of finder items. i.e. the return value of duplicate depends on the parameters sent to it.
AND... finder items are not very useful outside the finder. You'd be better off with aliases or POSIX paths.
So you probably need something like
on open (dropped)
tell application "Finder"
set duplicate_Finder_items to duplicate dropped
end tell
-- convert the Finder reference for each duplicate item to an AppleScript alias
set duplicate_item_aliases to {}
if class of duplicate_Finder_items is list then
repeat with i from 1 to number of items of duplicate_Finder_items
set the end of duplicate_item_aliases to (item i of duplicate_Finder_items) as alias
end repeat
else -- result was a single Finder item, not a list
set duplicate_item_aliases to {duplicate_Finder_items as alias}
end if
repeat with f in duplicate_item_aliases
set inf to (info for (f as alias))
set n to name of inf
display dialog "You duplicated a file. The duplicate is now known as " & n
end repeat
end open
The duplicate command allows for a location to be specified:
set theResult to duplicate reference ¬
to insertion location ¬
replacing boolean ¬
routing suppressed boolean
Parameter, Required, Type, Description
direct parameter, required, reference, the object(s) to duplicate
replacing, optional, boolean, Specifies whether or not to replace items in the destination that have the same name as items being duplicated
routing suppressed, optional, boolean, Specifies whether or not to autoroute items (default is false). Only applies when copying to the system folder.
to, optional, insertion location, the new location for the object(s)

Resources