How to create a new document in TexShop? - applescript

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

Related

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

How to get filename without extension from Omnigraffle?

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

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

programmatically updating iTunes track location

I would like to modify the filesystem path for tracks on itunes programmatically, so that I can apply a string transformation to some of the tracks locations (which are now stored in a different places on the filesystem).
I've tried using AppleScript to update the location property of the relevant tracks but I get an end-of-file error when calling "set mytrack's location to ..."
I've seen various other hacks online that involve exporting the entire track db, modifying it in XML, and then reimporting it - but that seems to lose too much metadata (such as playlists).
It would really help to see more of your code. Of particular interest is the value you are using and how it is derived. It would also be useful to see the exact error message you get (you should be able to copy the text out of the AppleScript error dialog sheet if you are running the program from Script Editor/AppleScript Editor).
The dictionary entry for the file track class shows its location property being a writable alias value. The problem you are probably running into is that you are not using an alias for the value.
The following code shows how one might change a track's location using an interactive prompt from choose file (which returns an alias):
set m to path to music folder
tell application "iTunes"
set trk to first item of selection
set l to location of trk
if class of l is alias then
set m to l
end if
set {d, a, n} to {database ID, artist, name} of trk
choose file with prompt "Choose the file to use for " & d & ": " & a & "—" & n default location m
set location of trk to result
end tell
The choose file method is not what you want though, since you are doing some kind of automated, string based pathname translation.
When working with pathnames in AppleScript, there are two kinds that you might use: POSIX and HFS. POSIX pathnames have slash delimited components (and allow colons inside any component). HFS pathnames have have colon delimited components (and allow slashes inside any component), and they usually start with a volume name component.
To convert a POSIX pathname stored in a variable str to an AppleScript alias, use the following expression:
POSIX file str as alias
To convert an HFS pathname stored in a variable str to an AppleScript alias, use the following expression:
alias str
For example:
tell application "iTunes"
set trk to first item of selection
set l to location of trk
set newPath to my computeNewPath(POSIX path of l)
set location of trk to POSIX file newPath as alias
end tell
to computeNewPath(pp)
-- presumably something interesting happens here
return pp
end computeNewPath
How to move media files (that are not "organized" by iTunes) to a different location while keeping the iTunes library database (iTunes Library.itl) intact:
Move files to new location (e.g., mv ~/MyMusic /Volumes/Huge/)
Create symlink in old location pointing to new location
(ln -s /Volumes/Huge/MyMusic ~/MyMusic)
Start iTunes (if not already running)
Select all tracks that were moved.
Run this AppleScript:
-- Mark missing tracks (and update database with real path of existing
-- tracks) in iTunes
-- Instructions:
-- * symlink new file location to old location (old location points to
-- new location, file exists)
-- * select tracks to scan/update
-- * run the script
-- * missing tracks are marked with (!) and all other track paths have
-- been updated to the real path (symlinks eliminated) in iTunes
tell application "iTunes"
set fx to fixed indexing
set fixed indexing to true
set Sel to the selection
repeat with i from 1 to number of items in Sel
set trk to item i of Sel
set loc to (get location of trk as text)
end repeat
set fixed indexing to fx
end tell
All tracks should now point to the correct location, and the symlink(s) can be removed. This can be verified by selecting Get Info on a track that was moved and verify that the path points to the new location.
If you didn't get the symlink correct iTunes will display (!) beside each missing track. To fix the issue, simply create a symlink in the old location pointing to the new location and run the script again. Hint: the Get Info dialog can be used to determine the old location of a missing track.
This worked on iTunes 11.0.5
Adding to the previous answer by millerdev I updated the script to work with MacOS Catalina and the new Music app. Just create a $HOME/Library/Music/Scripts directory and place it there.
tell application "Music"
set fx to fixed indexing
set fixed indexing to true
set Sel to the selection
repeat with i from 1 to number of items in Sel
set trk to item i of Sel
set l to location of trk
set location of trk to l
end repeat
set fixed indexing to fx
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