I am new to using applescript. I would like to write a script to add text to an existing file. Specifically, I would like to prepend/append a given document, e.g., prepend "Hello" [existing text in document] append "Goodbye." I found this example:
tell application "TextEdit"
activate
set theDesktopPath to the path to the desktop folder as text
set file_URLs_content to "HEEEELLOOOOOO"
make new document with properties {text:file_URLs_content}
save document 1 in file (theDesktopPath & "file.txt")
close document 1
end tell
But, this is not quite right because I do not need a new document, and I want to specify the location of the text (at the beginning of the file, or at the end).
I am open to other solutions, as well. Ultimately, I want to use the dictation command feature to run the script, perhaps via automator. Many thanks for your time!
TextEdit is not needed to write plain text to disk.
writeToDisk from theText into thePath given append:append writes plain text to disk
Parameters:
• theFile: a HFS path
• theText the text to be written
• with append: append the text - without append: overwrite from the beginning of the text
set theFile to (path to desktop as text) & "file.txt"
set file_URLs_content to "HEEEELLOOOOOO"
writeToDisk from file_URLs_content into theFile with append
on writeToDisk from theText into thePath given append:append
try
set fileDescriptor to open for access file thePath with write permission
if not append then set eof of fileDescriptor to 0
write theText to fileDescriptor
close access fileDescriptor
on error
try
close access file thePath
end try
end try
end writeToDisk
If you prefer to write UTF-8 encoded text change the write line into
write theText to fileDescriptor as «class utf8»
Related
I am trying to create a script that can create a text file with random information, and then duplicate the file onto any computer's desktop. Therefore, I might need a way of reading a user's directory.
This AppleScript code works for me using the latest version of macOS Mojave.
property fileName : "Test Document.txt" --value can be changed
property theText : "Random Information" --value can be changed
writeToAFile()
on writeToAFile()
set theFile to (path to desktop as text) & fileName
set theFile to POSIX path of theFile
try
set writeToFile to open for access theFile with write permission
write theText & linefeed to writeToFile as text starting at eof
close access theFile
on error errMsg number errNum
close access theFile
set writeToFile to open for access theFile with write permission
write theText & linefeed to writeToFile as text starting at eof
close access theFile
end try
end writeToAFile
Scratching my head after reading lots of different threads on this and tried a bunch of scripts but none seem to work.
I'd like to use Automator to automate Word 2016 conversion of a selection of docx files to pdf.
Used the following Automator Service:
Used the following script:
on run {input, parameters}
tell application id "com.microsoft.Word"
activate
open input
set doc to name of active window
set theOutputPath to (input & ".pdf")
save as active document file name theOutputPath file format format PDF
end tell
end run
Which results in error: Microsoft Word got an error: active document doesn’t understand the “save as” message.
The main issue is that input is a list. You have to use a repeat loop to process each file separately
I added a line to close the current document after having been converted
on run {input, parameters}
tell application id "com.microsoft.Word"
activate
repeat with aFile in input
open aFile
set theOutputPath to ((aFile as text) & ".pdf")
tell active document
save as it file name theOutputPath file format format PDF
close saving no
end tell
end repeat
end tell
end run
To prevent the problem discussed in #vadian's answer, save the file first to Word's default folder (that's usually ~/Library/Containers/com.microsoft.Word/Data/Documents) and then move the file somewhere else.
on run {input, parameters}
repeat with aFile in input
tell application "System Events"
set inputFile to disk item (aFile as text)
set outputFileName to (((name of inputFile) as text) & ".pdf")
end tell
tell application id "com.microsoft.Word"
activate
open aFile
tell active document
save as it file name outputFileName file format format PDF
close saving no
end tell
set defaultPath to get default file path file path type documents path
end tell
tell application "System Events"
set outputPath to (container of inputFile)
set outputFile to disk item outputFileName of folder defaultPath
move outputFile to outputPath
end tell
end repeat
return input
end run
I need to write a simple applescript that will be opened with an argument. That argument will be a file path. I need it to save this file path to a text file. I want to avoid using shell script.
I'm finding this surprisingly difficult, despite having done it in .sh and .bat already
Thanks very much!
Adam
//// the current code which is not working is below. the code begins with "on run" and ends with "end run" but for some reason this isn't being highlighted as code
on run argv
set this_PATH to (POSIX file argv)
tell application "TextEdit"
activate
make new document
set text of document 1 to this_PATH as text
save document 1 in "/Users/adamparkinson/Desktop/date.txt"
end tell
end run
I made this work:
#!/usr/bin/osascript
on run argv
tell application "TextEdit"
activate
make new document
tell document 1
set its text to (item 1 of argv as text)
save in posix file (item 1 of argv as text )
end tell
end tell
end run
I called it like this:
./SavePath.sh /Users/Me/Desktop/junk/Newstuff.txt (Of course I used chmod u+x SavePath.sh to make it executable.)
If this works for you, then please check of my answer as answered. :)
with applescript it is very easy to create application Drag and Drop without any script shell below a version Drag and Drop of your script
on open draggedItems
set this_PATH to quoted form of POSIX path of draggedItems
repeat with currentItem in draggedItems
tell application "TextEdit"
activate
set doc to open POSIX file "/Users/adamparkinson/Desktop/date.txt"
make new word at end of text of doc with data (this_PATH as text) & return
end tell
end repeat
end open
I'm trying to write in a TextEdit file already created.
The file is in rwxrwxrwx mode so no permission problem.
But when I execute my code, here is the error :
error "Network file permission error." number -5000 from file "/Users/me/Desktop/A directory/file.txt" to «class fsrf»
My code here :
-- Writing in the TextEdit file
set file_URLs_content to "HEEEELLOOOOOO"
tell application "TextEdit"
set theFile to "/Users/me/Desktop/A directory/file.txt"
set file_ref to (open for access file theFile with write permission)
set eof file_ref to 0
write file_URLs_content to file_ref
close access file_ref
end tell
And my file.txt is still empty, how can I avoid this error ?
The way you can avoid errors when writing text with TextEdit is to remember that it is a text editor. It already knows how to create and save text documents without generating errors. You don’t have to use (error-prone) open for access. You don’t have to use (error-prone) shell scripting. All you have to do is ask TextEdit to make you a text document with whatever contents you like and and save it wherever you like. TextEdit knows how to do that without generating file access errors (like open for access) or accidentally overwriting folders (like shell scripting.)
tell application "TextEdit"
activate
set theDesktopPath to the path to the desktop folder as text
set file_URLs_content to "HEEEELLOOOOOO"
make new document with properties {text:file_URLs_content}
save document 1 in file (theDesktopPath & "file.txt")
close document 1
end tell
The advantage of this method is it is faster and easier to write, it is less error-prone, the text file that you get as output has the same properties as text files that you create manually with TextEdit, and your script can now be easily expanded to include other apps. For example, the text content could come from another app or the clipboard, and the text file could be opened in another app or emailed after it is saved.
The most fundamental feature of AppleScript is sending messages to Mac apps in this way. If you want to convert a PNG to a JPEG, you don’t write a PNG decoder and JPEG encoder in AppleScript and open the PNG file for access and read it byte by byte and then encode a JPEG byte by byte. You simply tell Photoshop to open the PNG image and export it as a JPEG to a particular file location. The “open for access” command is a last resort for reading and writing files that you simply don’t have an app to read or write. The “do shell script” command is for incorporating command-line apps when you simply don’t have a Mac app to do the job, for example, you can do regex stuff with Perl. If all you are doing is working with text files, you not only have TextEdit, but you can also get the free TextWrangler from Mac App Store and it has a giant AppleScript dictionary for reading, writing, and editing text files.
You don't need TextEdit for that. Try this:
set the logFile to ((path to desktop) as text) & "log.txt"
set the logText to "This is a text that should be written into the file"
try
open for access file the logFile with write permission
write (logText & return) to file the logFile starting at eof
close access file the logFile
on error
try
close access file the logFile
end try
end try
Try:
set file_URLs_content to "HEEEELLOOOOOO"
set filePath to POSIX path of (path to desktop as text) & "file.txt"
do shell script "echo " & quoted form of file_URLs_content & " > " & quoted form of filePath
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