Save the contents of a PDF file as spoken audio using AppleScript - 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! :)

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:

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

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

Save thousands of html files as txt files, using firefox - how to automate this job?

I have thousands of html files, and need to save each of them as txt, using firefox.
If I do this job manually, I would open each html file in firefox, click the File menu, click the 'Save Page As' menu item, then select the format as 'TEXT', and save to local disk.
But how to automate this job?
Any script/tool can help this?
Thanks.
If your goal is to get firefox to strip the html out of each page and save just the text, then there are a ton of options. I'm not aware of any firefox add-ons that will be intelligent enough to loop over every file in a directory in order to perform a macro, so here are some options:
Refer to this SO question regarding how to use python to strip the html from each file. It provides examples for both the built in HTMLParser module and for using BeautifulSoup
Use Selenium to automate your webbrowser: http://seleniumhq.org/
If you know javascript, you can use PhantomJS:
http://www.phantomjs.org/, which is a headless web browser that you
drive with javascript scripts.
I have thousands of html files...
Do you actually have these files on-hand, or are they online?
...and need to save each of them as txt...
Any text editor should be able to save the data within (i.e. why use FireFox), and I think a straight rename of .htm or .html to .txt. will work (at least on any Windows system). Or do you mean: save just the displayed text of the HTML file?
EDIT:
First, start off with this link, which has a good explanation of how to get started with shdocvw, which you will need to do this.
Once you have the reference set up, using the functions
Function GetNewIE() As SHDocVw.InternetExplorer
and
Function LoadWebPage(i_IE As SHDocVw.InternetExplorer, i_URL As String) As Boolean
from the link (just copy into your project as described in the link) to load your individual html files, using a loop to get through each file. (Excel would be good for this, because you can put your list of files into the cells, and cycle through each cell to retrieve.) I have never done something like this with so many files, so I cannot guarantee this will work, unfortunately...
Dim IE As SHDocVw.InternetExplorer
Dim lRow as Long 'Long in case you have a LOT of files
Dim iFNum As Integer
Dim sFilePath As String
Set IE = GetNewIE
For lRow = 1 To 5000 Step 1 ' Assuming you have 5,000 html files, so 5,000 rows with the paths to each
sFilePath = ActiveSheet.Range("A" & lRow).Value ' This should also include the filepath. i.e. "C:\dir\..."
If LoadWebPage(IE, sFilePath) Then
iFNum = FreeFile(lRow)
Open sFilePath & ".txt" For Output As iFNum
Write #iFNum, IE.Document.InnerText
Close #iFNum
End If
Next lRow

Resources