AppleScript to pass dragged document to another application - applescript

Thanks to help received on these fine pages, my Mac has a little AppleScript to open a new session of Adobe Distiller.
do shell script "open -n -a " & quoted form of "Acrobat Distiller"
New question, asking for a small improvement to this. Can it be that if a .ps is dragged (or indeed, several are dragged) to the .app made by this .scpt, the new session of Distiller opens with that document (or those several documents)?
Thank you.

Save the following script as an application. If you run the application, it will let you choose files to open in a new instance; if you drop files on it, it will open them all in a new instance:
on run
set filesToOpen to choose file with multiple selections allowed
set fileListString to createUnixFileString(filesToOpen)
makeNewInstanceWithFiles(fileListString)
end run
on open droppedFiles
set fileListString to createUnixFileString(droppedFiles)
makeNewInstanceWithFiles(fileListString)
end open
on createUnixFileString(aList)
set fileString to ""
repeat with thisItem in aList
set fileString to fileString & " " & quoted form of (POSIX path of thisItem)
end repeat
return fileString
end createUnixFileString
on makeNewInstanceWithFiles(f)
do shell script "open -n -a " & quoted form of "Acrobat Distiller" & f
end makeNewInstanceWithFiles
If you want each file opened in a separate instance, call makeNewInstanceWithFiles for each file (making sure to get the posix path and include a space as a delimiter) instead of calling the createUnixFileString handler.

Related

Applescript Application will Add Key/Value Pair to Multiple Applications

My goal was to create a drag'n'drop AppleScript application that will replace NSUIElement key/value pairs in the application's info.plist file. This will effectively hide application's icons from the Dock & Application-Switcher.
So far this creates an extra 'Info.plist' file called "Info.plist.plist" in ƒContents, with the NSUIElement value as the only key/value pair (it will not ammend the original file). Can you suggest a solution?
-- OS X 10.3.9
on open ItemList
repeat with x in ItemList
set apppath to POSIX path of x
set infile to quoted form of (apppath & "Contents/Info.plist")
do shell script "defaults write " & infile & " 'NSUIElement' '1'"
end repeat
end open
I'm not sure that I have enough info to come up with an answer, but my guess is that
do shell script "defaults write " & infile & " 'NSUIElement' '1'" with administrator privileges
will work.

Remove multiple PDF passwords with workflow or applescript in a folder

How do I remove passwords from multiple PDF files using Applescript or by creating a Workflow in OS X?
My scenario is that I have multiple password protected PDF files in a folder. I know the passwords for all, which is same. I want to be able to run a Workflow on this folder so that all PDFs inside it are unlocked by the workflow.
OR run an Applescript shell code on all these files at once
I also preferably want to be able to create a way where putting / moving / pasting any PDF in the folder automatically unlocks it :)
Help appreciated !!
Update:
I have tried pdftk. The following code works awesome in Terminal, once pdftk is installed
pdftk secured.pdf input_pw foopass output unsecured.pdf
Now I want to be able to create a workflow that runs this command on selected files or on all the files in a folder
The AppleScript command to execute a shell script is do shell script...
So something like this:
do shell script "pdftk secured.pdf input_pw foopass output unsecured.pdf"
should work.
At this point I see 2 options:
write an AppleScript script that ask the user for the folder or get it from the Finder selection and then execute the command for each file in the folder;
write an Automator workflow that get the files from the folder using already available actions and then attach a new action that execute the AppleScript script.
For option 2 you can set an Automator workflow as in the following image.
Have you heard of "Folder Actions"? It's a way to attach an applescript to a folder so that whenever a new file is added to the folder the applescript is run. A quick google search turned up this which will give you directions on how to set it up. You can do more google searching if you still have questions.
Here's an applescript you can use with folder actions. I didn't test it but it should work (it's basic code). This will do its stuff on only pdf files. Other files you add to the folder will be left alone. NOTE: you have to put in your values for the first 4 variables of the script.
Good luck.
on adding folder items to theFolder after receiving theItems
-- enter your values here
set pdftkPosixPath to "/usr/bin/pdftk"
set pWord to "foopass"
set appendedName to "_unlocked" -- text to append to the file name
set shouldTrash to true -- true or false, move the locked file to the trash after unlocking?
set fContainer to theFolder as text
repeat with anItem in theItems
try
tell application "System Events"
set fName to name of anItem
set fExt to name extension of anItem
end tell
if fExt is "pdf" and fName does not contain appendedName then
set baseName to (text 1 thru -5 of fName) & appendedName & ".pdf"
set newPath to fContainer & baseName
do shell script (quoted form of pdftkPosixPath & space & quoted form of POSIX path of anItem & " input_pw " & quoted form of pWord & " output " & quoted form of POSIX path of newPath)
if shouldTrash then
tell application "Finder" to move anItem to trash
end if
end if
end try
end repeat
end adding folder items to
EDIT: here's how you can ask for a password. Note that if you want to see the text then remove "with hidden answer".
display dialog "Enter a password:" default answer "" with icon note with hidden answer
set theAnswer to text returned of the result
if theAnswer is not "" then set pWord to theAnswer

AppleScript: problems reading from a file

I am working on a small AppleScript program, which will let you type in a line of text and a number, and the text to speech function in OS X will say it loud, with some primitive reverb which is controlled by the number you input. (Study my code for details.)
Everything works out fine, except for one thing. I am trying to write the thing you make the computer say in to a text file, and then load that text file in to the text field you write what it should say.
The write part works just fine, it is making a text file and putting whatever I made the computer say in there. The problem is with the read.
As it is now, the read part looks like this:
try
open for access prevSFile
set defToSay to (read prevSFile)
end try
Nothing happens at all. If I try to remove the 'try', it gives me the error -500, and the program stops.
Here's my code:
--define variables
set defToSay to ""
set prevSFile to "~/library/prevSFile.txt"
--Fetch info from save file:
try
open for access prevSFile
set defToSay to (read prevSFile)
end try
--Display dialoges:
display dialog "What do you want to say?" default answer defToSay
set whatToSay to the text returned of the result
display dialog "How many times do you want to overlay it?" default answer "5"
set overlays to the text returned of the result
--Create/edit save file:
do shell script "cd /"
try
do shell script "rm " & prevSFile
end try
do shell script "touch " & prevSFile
do shell script "echo " & whatToSay & " >> " & prevSFile
--Say and kill:
repeat overlays times
tell application "Terminal"
do script "say " & whatToSay
end tell
delay 0.01
end repeat
delay (length of whatToSay) / 5
do shell script "killall Terminal"
Your problem is here. AppleScript paths do not use "/" and AppleScript certainly doesn't know "~".
"~/library/prevSFile.txt"
Here's what that part of the code should be...
set prevSFile to (path to home folder as text) & "Library:prevSFile.txt"
try
set defToSay to (read file prevSFile)
end try
Now that you have the proper path for AppleScript you need to fix the paths for the shell commands. Note that you do not need to rm and touch a file every time. Just use ">" when you redirect the echo command and that will overwrite the file. You also need to use the "quoted form" of paths in case there's spaces.
do shell script "echo " & quoted form of whatToSay & " > " & quoted form of POSIX path of prevSFile
However you're doing a lot of unnecessary stuff in that script. Here's how I would write your code. Good luck.
property whatToSay : ""
property numberOfTimes : 5
--Display dialoges:
display dialog "What do you want to say?" default answer whatToSay
set whatToSay to the text returned of the result
repeat
display dialog "How many times do you want to overlay it?" default answer (numberOfTimes as text)
try
set numberOfTimes to the (text returned of the result) as number
exit repeat
on error
display dialog "Please enter only numbers!" buttons {"OK"} default button 1
end try
end repeat
repeat numberOfTimes times
say whatToSay
end repeat

Using Automator or Applescript or both to recursively print documents to PDF

I have a massive set of files (4000+) that are in an old Apple format (Appleworks). My employed needs them all updated to PDF. By opening the documents in Appleworks and using the system print dialogue, I can save them to PDF—this is ideal. I'm a complete nub with Applescript/Automator, however.
Using a Python script I was able to gather all the Appleworks files from my bosses computer and put them in a directory; each file is then in a subdirectory with a .txt file containing its original location (where, eventually, I will have to put them back).
I need the script to move recursively through this massive directory, getting every file that's neither a folder nor a .txt document, and save it to PDF in the same directory in which the original file was found. ie.
/Appleworks/Boss_File_1/
will contain
/Appleworks/Boss_File_1/Boss_file_1.cwk and
/Appleworks/Boss_File_1/path.txt
But must eventually also contain /Appleworks/Boss_File_1/Boss_File_1.pdf
I can get half way with either solution, but don't know how to make them work together. The Applescript I'm using looks like:
set appleworksFolder to choose folder
tell application "Finder"
set folderItems to (files of entire contents of appleworksFolder)
repeat with I from 1 to number of items in folderItems
set the_doc to item I of folderItems
if name of the_doc is not "path.txt" then
try
tell application "AppleWorks 6"
open the_doc
tell application "System Events"
tell process "Appleworks"
keystroke "p" using command down
click menu button "PDF" of window "Print"
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
click button "Save" of window "Save"
end tell
end tell
end tell
end try
else
tell application "Finder"
delete the_doc
end tell
end if
end repeat
end tell`
This opens the print dialogue but never gets any further and I have no idea why. I realize this script also doesn't deal with putting the document back in its original folder, but in Applescript I could easily enough do this if I could get past the actual printing-to-PDF bit.
Meanwhile, in Automator, using this workflow:
Get Specified Finder Items
Get Folder Contents
Filter Finder Items (by kind and then by file extension is not .txt)
Open Finder Items (with Appleworks)
I then am stuck; using the actual Print Finder Items and choosing Adobe PDF seems to actually do nothing at all, and recording myself using the print to pdf process live is useless because I don't know how to get Automator to retain the path the file originated from and ensure it prints to it.
If anyone can help me put this together somehow, I'd be enormously grateful. Thanks.
Convert using Pages
If you have Pages (part of iWork), it can open .cwk files and save them as PDF: just replace your if block with this:
if (the_doc's name extension is not "txt") then
set newName to my makeNewFileName(the_doc, "pdf")
try
tell application "Pages"
open (the_doc as alias)
set thisDoc to front document
save thisDoc as "SLDocumentTypePDF" in newName
close thisDoc saving no
end tell
on error
display dialog "Error: cannot export " & (name of the_doc) & " to PDF."
end try
end if
(you will need this custom function makeNewFileName):
(* prepare new file name with extension ext *)
on makeNewFileName(finderItem, ext)
tell application "Finder"
set fname to finderItem's name
set thePath to (finderItem's container) as alias as text
return (thePath & (text 1 thru ((length of fname) - (length of (finderItem's name extension as text))) of fname) & ext)
end tell
end makeNewFileName
(complete working script)
GUI scripting
Alternatively, you could do GUI scripting upon AppleWorks as you attempted, but it has the disadvantage that you cannot programmatically specify where to save the PDF file.
This snippet works for me:
tell application "AppleWorks 6"
open the_doc
activate
tell application "System Events" to tell process "AppleWorks"
keystroke "p" using command down
delay 1 -- or longer, if it takes longer
click menu button "PDF" of window "Print"
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
delay 1 -- or longer
click button "Save" of window "Save"
end tell
end tell
Unfortunately, AppleWorks doesn't seem to properly listen to AppleScript's close command, therefore you may need to close the file by also simulating the cmd+W keystrokes.
Try this:
set appleworksFolder to choose folder
set thePath to POSIX path of appleworksFolder as string
tell application "Finder"
set folderItems to files of appleworksFolder
repeat with aFile in folderItems
set {name:fileName, name extension:nameExtension} to aFile
set filePath to POSIX path of (aFile as alias) as string
if nameExtension is not "txt" then
set theLocation to POSIX path of (aFile as text)
set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
set destLocation to (thePath & baseName & ".pdf")
set theCommand to "/System/Library/Printers/Libraries/./convert -f \"" & filePath & "\"" & " -o " & "\"" & destLocation & "\"" & " -j \"application/pdf\""
do shell script theCommand
else
tell application "Finder" to delete aFile
end if
end repeat
end tell
I needed to do this today on Mountain Lion with a bunch of RTF receipts; here's how I did it:
#!/bin/bash
for file in *.rtf ; do
filename=$(basename "$file")
/usr/sbin/cupsfilter "$file" > "$filename.pdf"
done
Worked great; super easy. No Automator or AppleScript silliness.

Delete file without playing sound in Applescript?

tell application "Finder"
set deletedfile to alias "Snow Leopard:Users:test.pdf"
delete deletedfile
end tell
The problem is I repeatedly call this script from my Cocoa application so the sound is played repeatedly too. Is it possible to disable that sound ?
Since the trash is just an invisible folder inside your home folder you can do this...
set myFile to (path to desktop folder as text) & "myFile.txt"
set trashFolder to path to trash folder from user domain
do shell script "mv " & quoted form of POSIX path of myFile & space & quoted form of POSIX path of trashFolder
one simple way (doesn't move to Trash)
do shell script "rm '/Users/test.pdf'"

Resources