Applescript + Microsoft Word -- file types for "save as"? - macos

I am trying to write a script to save a Word document in the single-file webpage (.mht) format. I am up to the part where I write the actual "save" command, and I'm stuck there. This is what I am trying to do:
# the_file is a variable which has been set here
tell application "Microsoft Word"
activate
open the_file
save the_file as [type]
end tell
The open part works just fine. But I don't know what to put in for the save type. Perhaps more importantly, I don't know where I can find a list of the available types. Can anyone help?
EDIT: A commenter suggested the word dictionary; I found the following there but don't know how to interpret it [I'm an AS noob].
[file format format document97/‌format document97/‌format template97/‌format template97/‌format text/‌format text line breaks/‌format dostext/‌format dostext line breaks/‌format rtf/‌format Unicode text/‌format Unicode text/‌format HTML/‌format web archive/‌format stationery/‌format xml/‌format document/‌format documentME/‌format template/‌format templateME/‌format PDF/‌format flat document/‌format flat documentME/‌format flat template/‌format flat templateME/‌format custom dictionary/‌format exclude dictionary/‌format documentAuto/‌format templateAuto] : The format in which the document is saved.

Try format web archive. Of all the formats listed, that one looks the most likely.

1- You must specify a document when using the save command, not the file path.
For better control, use the open command with the property file name, it return the document object.
When using this : open the_file, it return nothing, in this case you must use front document, but it's unreliable, for example if another document opens after.
2- Word does not change the extension when using the save command in Applescript, the script must replace the extension.
Also, I recommend the command save as to have more options instead of save.
Answer updated : format HTML instead of Web archive
set the_file to (choose file)
tell application "Microsoft Word"
set thisDoc to open file name (the_file as string)
set tName to my removeExtension(name of thisDoc)
-- save in the same directory
save as thisDoc file format format HTML file name (tName & ".htm") with HTML display only output
close thisDoc saving no
end tell
on removeExtension(t)
if t does not contain "." then return t
set tid to text item delimiters
set text item delimiters to "."
set t to (text items 1 thru -2 of t) as string
set text item delimiters to tid
return t
end removeExtension
If you don't want HTML display only output, use without HTML display only output

Related

Saving a text file in Emeditor with a custom name using Javascript

I want to save a document without popping up the "Save As" window. The name of the file document be specified before saving, how can I do that?
I know that there is a command called document.Save( [ strName ] ); Sadly, it allows me to save the document with numbers only. I want to save the document with words (a,b,c,..) as well.

Use paragraph-separated list to remove songs from iTunes playlist

I can get songs from a playlist based on a text file with a different title in each paragraph, but can I use another text file to delete any songs in playlist 'SongList' whose title matches one of the titles in my 'SongList' text file?
Know how to read the text file
set mySampleText to read file "Macintosh SSD:path:to:file.txt"
And how to get paras of it
set paras to paragraphs of mySampleText
But I can't find a way to delete any tracks who share a name with any line of mySampleText.
Can anyone help?
Thanks
Tardy
The script below find tracks with same title as in your text file and delete.
Be careful, there is no reverse to the delete action !!!
set FText to choose file "select your text file" -- select text file
set mySampleText to read FText -- read txt file
set Paras to paragraphs of mySampleText
tell application "iTunes"
tell playlist "My preferred playlist"
repeat with aSong in Paras -- loop to each title
set myTracks to (every track whose name is aSong)
if (count of myTracks) > 0 then -- only if found
set myTrack to item 1 of myTracks -- take first item found
Delete myTrack
end if
end repeat
end tell
end tell
You may have to manage the case when you have multiple tracks with the same title. Script above only takes the 1st track found.
I strongly reccomand that you replace the "delete" by "play" during debuting !! (to be safe).
Last, but not least, make sure your txt file is properly encoded if you have special characters in your titles.

AppleScript loop to ask (3) questions, select multiple finder files, combine results into a text file

I am looking for some help getting an apple script setup. I have been trying to copy and past from different examples on the web to no avail. I am setting up a journal / diary for a family member and need to have a text file that contains the following information.
The AppleScript will display a dialogue box asking for three things:
The name of an event
The date of the event
A description for the event
Each of those would be stored as a separate variable.
Then the script would ask for a selection of files from the Finder, nothing nested, just a selection of 15 - 30 files all contained in the same folder.
Finally a new TextEdit document would be created
The beginning of the document would have the (3) variables mixed in with some default text.
The middle of the file would be filled in with a repeat loop based on the number of files selected from the finder. Their file paths would be mixed in with additional default text.
The last section would be default text only, no variables required.
I am sure my description is way more complicated than the script will probably be. Would anyone be able to provide this script for me? It would be most appreciated.
Here is a rough idea what the final thing would look like. The bold areas are the variables.
The activity of the day was scuba diving.
The date you went scuba diving was January 1, 2016.
This is a description of your event. The day was quite beautiful and the water was perfect. You were able to see a wide variety of fishes!
These are the locations of the files from this event.
The first file is /events/scuba/scuba1.txt
These are the locations of the files from this event.
The first file is /events/scuba/scuba2.txt
These are the locations of the files from this event.
The first file is /events/scuba/scuba3.txt
This was a summary of your scuba diving activity. These memories will last a lifetime!
I appreciate the help with this. And if the family member in question was able to provide a thanks, know that they would as well.
You can do that like this:
set evName to text returned of (display dialog "The name of an event" default answer "")
set evdate to text returned of (display dialog "The date of the event" default answer "")
set evDesc to text returned of (display dialog "A description for the event" default answer "")
set theText to "The activity of the day was " & evName & return & "The date you went " & evName & " was " & evdate & return & evDesc & return & return
set x to choose file with multiple selections allowed
set def1 to "These are the locations of the files from this event."
set def2 to "The first file is "
repeat with i in x
set theText to theText & def1 & return & def2 & (POSIX path of i) & return
end repeat
set theText to theText & return & "This was a summary of your " & evName & " activity. These memories will last a lifetime!"
tell application "TextEdit"
make new document with properties {text:theText}
activate
end tell
May I suggest an alternative solution using Evernote?
You could create a "template" note using a table to fill in the activity, date, and description. Any time you want a new journal entry, just select the template, and go to Note > Copy to Notebook.
Then you can attach and/or import the text of the files.
This would also allow you to add images and other attachments, and search much easier. And of course it is easy to share.
Let me know if you'd like more details.
Screenshot of example:

How can I get a URL from a selected hyperlink using Applescript?

I’m attempting to create an Applescript that will grab the URL from a selected hyperlink.
For some backstory: the system that my company has in place doesn’t play well with generating reports, so I created a script into which I can paste a list of URLs, at which point Safari will go through each page and select all the data, copy it, and parse out what I need.
However, each page that I’m parsing has a link on it that says, for example, “Edit”. If I post it into, say, Pages, the hyperlink is preserved. It would GREATLY speed up my flow if I could somehow get the URL contained in that hyperlink.
Any ideas?
Drew, I suspect you got no answer because it's a little difficult to discern what you're wanting. But, here is a script that will grab the raw text of a web page, and then find the first href hyperlink that is named "Edit", and then return the target URL that it's linking to. It uses CURL to pull the content and offset to find the link name. You might have to adjust the tag identifiers surrounding the Link Name you're searching for.
property baseURL : "http://www.mycoolsite.index.html"
property linkName : "Here"
set rawHTML to do shell script "curl '" & baseURL & "'"
set theOffset to offset of ("\">" & linkName & "</a>") in rawHTML
set rawHTML to text 1 thru (theOffset - 1) of rawHTML
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "http://"
set targetURL to (text item -1 of (text items of rawHTML))
set AppleScript's text item delimiters to otid
return targetURL

Save the contents of a PDF file as spoken audio using AppleScript

I have built a simple script which takes a string and converts it to an audio file:
say "This is only a test." using "Fred" saving to file (((path to desktop) as string) & "audio.aiff"
I'd like to edit the script so that instead of using the string "This is only a test" it would fetch a PDF document and use the text in the document to create an audio file.
Skim, a free PDF viewer, might be of interest to you. It's scriptable and features an AppleScript command to extract the text from certain PDF pages. Once you've downloaded Skim, you can run this script:
tell application "Skim"
open (POSIX file "/path/to/PDF/document.pdf")
set the stuff to (get text for page 1 of document 1) as string --change 'page 1' to whatever page number you need
--if you want all the pages, just use 'every page'
end tell
say the stuff using "Fred" saving to file (((path to desktop) as string) & "audio.aiff")
Happy coding! :)

Resources