I want AppleScript to loop through a set of of RTF files in folder and save them as HTML files.
This is my simple code so far. The XXXX is where I'm struggling:
tell application "Finder"
set source_folder to choose folder
set aList to every file in source_folder
repeat with i from 1 to number of items in aList
tell application "TextEdit"
set aFile to (item i of aList)
save as aFile XXXXXXXXX
end tell
end repeat
end tell
I'm really new to this... any help much appreciated.
You don't need TextEdit for this. There is a command line program textutil which will do the job without all the opening and saving stuff required with TextEdit. We can fix your TextEdit script (it has a few errors) but try this first and let us know if it does the job for you. The html files will have the same name but with the html extension and will be located in source_folder. The ouput path can be changed in the code by using the "-output" switch of textutil. See "man textutil" if you want to look at everything it can do.
And a general question... what is a RTD file? Do you mean rtf or rtfd? Textutil will work with rtf/rtfd but not rtd, so I hope that isn't really your file type.
set source_folder to choose folder with prompt "Choose a source folder."
set output_folder to choose folder with prompt "Choose an output folder."
tell application "Finder"
set theFiles to (files of entire contents of source_folder) as alias list
end tell
repeat with aFile in theFiles
tell application "Finder"
set fileName to name of aFile
set fileExt to name extension of aFile
end tell
set outputPath to (output_folder as text) & text 1 thru -((count of fileExt) + 1) of fileName & "html"
do shell script "/usr/bin/textutil -convert html -output " & quoted form of POSIX path of outputPath & space & quoted form of POSIX path of aFile
end repeat
You mention you are new to applescript, so I'll give you some general pointers you should keep in mind when writing applescript code.
Avoid putting tell blocks of code inside each other. You have tell app TextEdit inside tell app Finder. That's bad. Doing this is a source of many conflicts because you are basically telling the Finder to tell TextEdit to do something. That's not good because commands can get confused and it's really hard to debug these kinds of issues. So keep your tell blocks separate.
Avoid telling an application to perform a command that is not in its applescript dictionary. You should only tell an application to do commands that it knows and an application only knows about the commands in its dictionary. So for example, you are telling the Finder to "choose folder". The Finder does not know that command. That's an applescript command. So doing as you have done is another possible source of errors. In this case that's a simple command and it will work but in general avoid doing this.
Regarding the Finder, you should avoid using it too much. The Finder is a major program on your computer and is often busy doing computer related stuff. As such it's best to only use it when necessary. As an example you can see in my code that I removed the "choose folder" and the repeat loop from the Finder. I purposely appended "as alias list" to the end of the Finder command to make the list of files usable outside of the Finder tell block of code. Of course use the Finder if needed but it's best to not use it if you don't need it.
Use the applescript dictionary of your applications. As mentioned above, the dictionary lists all of the terms and the syntax that an application understands (granted the dictionaries are difficult to understand but you will get better at it the more you use them). Under the file menu of AppleScript Editor choose "Open dictionary" and a list of all the applications that understand applescript is shown. Choose an application from that to see its dictionary. So for example, you are trying to figure out TextEdit's "save as" command. You can usually get good direction from the dictionary so you should take a look at that. Use the search field to search!
So I hope that helps! Good luck.
Related
I worked out two scripts to do that, but none of them is working (I even don't get shown the "on error" message. Maybe it's because of the pre-defined folder as a variable? As for simplicity and a cleaner code I didn't want to write the complete path again, so I used that folder variable. In case it is relevant, the DataHubFolder variable was defined like this earlier: set DataHubFolder to cloudDocs & "Current Projects:Data Hub:"
Here are the codes I used:
Code #1:
try
tell application "Finder"
set f to every file ¬
of folder ¬
of DataHubFolder & "Distrokid:"
delete f
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Code #2:
try
tell application "Finder"
delete (every item of folder (DataHubFolder & "Distrokid:"))
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Would be so happy about some tips.
Thanks a lot.
I have a weird issue with mac automator (on Mojave).
What I want: Take a pdf > save each page as png to same folder as pdf
What happens: Takes a pdf > all pngs are saved to desktop
Here is the automator after a test run:
You can see on the screenshot how variable "pdfPath" actually is set correctly to Test folder. But then after moving it does remove the files out of system folder, but not to pdfPath but to Desktop. Important: if I manually pick any folder, it will save it there and not to desktop, so still somehow related to the pdfPath variable?
Anybody an idea why it happens and how to fix?
Automator just gets frustrating like this at times. The Move Finder Items action doesn’t appear to work with variables in this situation, even though you can drag and select them - when using a variable it defaults to the Desktop. As a workaround you can replace the Move Finder Items action with a Get Value of Variable for pdfPath to add it to the input items, then add a Run AppleScript action to do the moving:
on run {input, parameters}
if (count input) < 2 then error "No Items to move.“
set destination to last item of input
tell application "Finder" to move (items 1 thru -2 of input) to destination
end run
You might also have to clean out previous results from the temporary folder to keep the rename action happy.
Ok, I figured it out with some help in the meantime. For anybody coming across and looking for solution, the path I saved in the Applescript part was showing up nicely in the result, but in the end, the path was in the wrong format. Following code works like a charm:
on run {input, parameters}
tell application "Finder" to return POSIX path of (container of (item 1 of input) as alias) as text
end run
I'm creating an automator pdf print plugin.
When you choose the print plugin the filename to the pdf is the input (normally /var/something /documentName.pdf)
I would like to get the documentName to use it later in an Rename Finder Item.
I'm using atm applescript to accomplish this.
on run {input, parameters}
tell application "Finder"
set fileName to name of ((POSIX file input) as alias)
end tell
return fileName as string
end run
The problem is that this only works when I put an Ask for Text Action before the applescript which displays the posix path.
If I remove the Ask for Text action the applescript fails.
The workflow is at https://www.dropbox.com/s/jp4t9pen3gvtyiq/Rename-Action.workflow.zip
I guess it is something simple but this is the first applescript / automator workflow I'm creating.
As I fail on commenting
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken post below.
Thanks!
I created a test workflow with this AppleScript:
on run {input, parameters}
tell app "System Events"
display dialog ((class of input) as string)
end
return input
end run
That displayed "list". I then modified it to:
on run {input, parameters}
tell application "System Events"
display dialog ((class of first item of input) as string)
end tell
return input
end run
That displayed "alias".
So, the input to a PDF workflow is a list of aliases. Write your script with that in mind and it should work. For example, this works:
on run {input, parameters}
tell application "System Events"
display dialog ((name of first item of input) as string)
end tell
return input
end run
When working with AppleScript, it can really help to forget everything you know about file paths. If you think in paths, you will be doing path-math in your head and all of it is unnecessary work. What you want to work with is objects. When doing file operations, you work with alias objects.
If you look at the PDF you are working with in Finder, and go File ▶ Make Alias then you’ll create an alias file. You can drag that alias file all around the file system of the disk it is on, put it in any folder, and when you open the alias file, it will still always open your original PDF, even if you forget what path name your original PDF file has, and even more importantly: the alias will open the PDF even if the PDF has moved to somewhere else in the file system. An alias does all that work for you. You don’t need to know the path names.
In AppleScript, rather than working with files, you work with aliases, and whatever you do to an alias is also done to the original file. So you don’t need to know the path name of a file to change its name — you only have to have an alias of it to work on. You store that alias in a variable.
So what you want to do is set the input PDF alias to a variable, and then later, that variable is what you give Finder to rename. You don’t have to know any paths. It doesn’t matter where the input PDF is stored on the file system — the alias will take care of that.
Here is an example AppleScript that demonstrates the principle of taking an alias as input, and then later renaming that alias (and thus, the original file:)
tell application "Finder"
set theInputFile to (choose file)
-- do a workflow here
set the name of theInputFile to "Renamed" & "." & the name extension of theInputFile
end tell
Here is a line-by-line description of the above script:
the opening tell block that specifies we are talking to Finder
show the user a choose file dialog box, and set the alias that is returned by that dialog box to a variable called “theInputFile”
a comment that is a placeholder for whatever workflow steps you might want to do
rename theInputFile to “Renamed” and its original file extension
quit talking to Finder
And even where you want to work with the folder that contains your original input file, or want to know what disk the input file is on, you still don’t need to work with path names:
tell application "Finder"
set theInputFile to (choose file)
set theContainingFolder to open the container of theInputFile
set theInputFileDisk to the disk of theInputFile
end tell
And if you want to know what kind of file the input file is, you don’t have to look at the filename extension and figure it out, you can just say:
set theInputFileKind to the kind of theInputFile
if theInputFileKind is equal to "Portable Document Format (PDF)" then
-- do stuff
end if
And if you want to work in specific folders, such as the home folder, there are special properties for that, like “the path to the home folder” so that the following script opens “~/Public/Drop Box” on any system, no matter what the user name:
tell application "Finder"
activate
set theHomeFolder to the path to the home folder as alias
set theDropBoxFolder to folder "Drop Box" of folder "Public" of theHomeFolder
open theDropBoxFolder
end tell
You can walk around disks and folder structures as objects as shown above, so again, there is no need to think in paths. Think in terms of setting variables to objects that you want to interact with.
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken's post
Thanks!
I need to delete a file off of my Cincinnati Laser from FileMaker. It gets it there via a FMScript that export field contents of a container to this location. So I know the file name and the path its all built in a calculation field on that record. But I don't know how to get that info into an Applescript using the FM12 "Perform AppleScript" script step
When I hard code the path and file name (Shown Below) it works.
set xpath to "Titanium Brain:Users:smartin:Desktop:Laser:1512-clr-c.cnc"
tell application "Finder"
delete file xpath
end tell
When I try to pass the field contents (Shown Below) it doesn't work.
set xpath to Laser::gCNCPath
tell application "Finder"
delete file xpath
end tell
What am I missing?
The problem with Perform AppleScript with calculations is always the managing of quotes and returns. Putting exactly the following into the 'Calculated Applescript' box of the 'Perform Applescript' script step should work for you:
"set xpath to " & Quote ( Laser::gCNCPath ) & ¶ &
"tell application \"Finder\"¶" &
"delete file xpath¶" &
"end tell"
Honestly, though, the whole thing gets pretty ugly pretty quickly. If you have security locked down appropriately, I'd be more inclined to put the whole script into the Laser::gCNCPath field
set xpath to "Titanium Brain:Users:smartin:Desktop:Laser:1512-clr-c.cnc"
tell application "Finder"
delete file xpath
end tell
And then, for the Perform Applescript, you only need to call the field:
Laser::gCNCPath
Whenever I've needed to pass information from FileMaker to AppleScript (and I'll admit, it's been a while since I've done so), I've used the Native AppleScript field in the Perform AppleScript dialog box and used a global field to store the "parameter" that AppleScript needs and used AppleScript to pull that information.
set xpath to cell "gCNCPath" of layout "Laser" of current file -- double check this syntax, I'm working from memory
tell app "Finder" to delete file xpath
I need to open multiple Safari (or open the tab is OK) based on the read result.
For example, if a file has
http://a.com
http://b.com
I want to open a.com and b.com using Safari.
How can I do that with Mac/AppleScript?
Maybe I can run python calling "open -a Safari "http://a.com", but I guess AppleScript is the tool for this kind of job.
not sure about python but this will read a text file and open windows let me see if I can get tabs for you though
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
repeat with aline in locations
if length of aline is greater than 0 then
tell application "Safari"
make new document at end of documents
set URL of document 1 to aline
end tell
end if
end repeat
EDIT:
Ok this is better and it opens them in tabs of a single window
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
tell application "Safari"
activate
set adoc to make new document
end tell
repeat with aline in locations
if length of aline is greater than 0 then
tell application "Safari" to make new tab at end of window 1 with properties {URL:aline}
end if
end repeat
New Addtion
this is yet another way based on regulus6633's post in conjunction with mine
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
repeat with aLocation in locations
tell application "Safari" to open location aLocation
end repeat
If you want it to specifically open the links in Safari then mcgrailm's solution is good. However, you don't need the Finder for the first part so take that code out of the Finder tell block. There's no need to tell the Finder to do something that applescript can do itself.
However, you probably want to open the links in whatever browser is the user's default browser. It may be Safari or Firefox etc. You can do that with the "open location" command. So something like this is probably what you want...
set theFile to choose file with prompt "Pick text file containing urls"
set locations to paragraphs of (read theFile)
repeat with aLocation in locations
try
open location aLocation
end try
end repeat