How can I copy System Info to clipboard? - applescript

I am creating a program that copies System Info to the clipboard. I have tried setting a variable to System Info and copying that variable but it only copies my AppleScript version (2.2.4). Please help! My code is below:
set a to system info
set the clipboard to a

You need to coerce the record to a list...
set a to (system info) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
set the clipboard to a

Related

Applescript to prepend and append text to existing textedit document

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»

AppleScript : Trying to write in a TextEdit file

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

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

Change Applescript into single line NSApplescript source

Im building an app for learning purposes of using applescript to send multiple commands during an action. Below is the code I'm messing with but I have stripped out the actions in between the "" and replaced them with numbers. Everything works fine in applescript but making this into an NSApplescript initwithsource: line has been a bother.
tell application "Terminal"
activate
set currentTab to do script "1"
do script "2" in currentTab
do script "3" in currentTab
do script "4" in currentTab
delay 0.5
tell application "Finder" to set visible of process "Terminal" to false
end tell
What is the best way to combine this applescript into a single line? Thanks!
"What is the best way to combine this applescript into a single line?"
Use AppleScript? :-D
First, in AppleScript Editor, open the Preferences window and click the option to Show Script menu in menu bar.
Then choose Open Scripts Folder from the script menu item up in the upper right of the screen.
Create a new AppleScript .scptd document with the following script:
tell application "AppleScript Editor"
set string_ to text of first document
-- make a list with each line of the script
set stringLines to paragraphs of string_
set originalDelims to AppleScript's text item delimiters
-- add newlines
set AppleScript's text item delimiters to "\\n"
-- now combine the items in the list using newlines
set stringNewlines to stringLines as string
set AppleScript's text item delimiters to "\""
set stringNewlines to text items of stringNewlines
set AppleScript's text item delimiters to "\\\""
set stringNewlines to stringNewlines as string
set AppleScript's text item delimiters to originalDelims
set stringNewlines to "#\"" & stringNewlines & "\""
set the clipboard to stringNewlines
end tell
(Note that this script isn't perfect: it works fine for simple scripts like the one you provided, but isn't able to convert itself).
Save that as a script in the Scripts folder you revealed earlier.
Then open your script document you want to convert and make it the front document in AppleScript Editor. Then invoke the conversion script by choosing it from the Script menu.
Given the script you provided, it should produce the following constant NSString:
#"tell application \"Terminal\"\n activate\n set currentTab to do script \"1\"\n do script \"2\" in currentTab\n do script \"3\" in currentTab\n do script \"4\" in currentTab\n delay 0.5\n tell application \"Finder\" to set visible of process \"Terminal\" to false\nend tell\n"

AppleScript copy and paste file with right-click

Sorry I don't have more of a foundation but I know nothing of AppleScript. Basically I want to right-click on a file or folder and run a script that will copy it to a new location with a constant directory structure. So obviously the scripting is way off, but something like this..
path = /Volumes/RENDERS/ThisShow/ThisShot/ThisShow_ThisShot_v10/
newPath = from 'path' ReplaceText("/Volumes/RENDERS/" , "/Volumes/Raid-Renders/")
tell application "Finder" to duplicate file 'path' to 'newPath' with replacing
In this example "path" would be the file or folder that was right clicked on when launching the script. The new folder in this example would be "/Volumes/Raid-Renders/ThisShow/ThisShot/ThisShow_ThisShot_v10/". So the idea is that it would copy "ThisShow_ThisShot_v10" folder and its content to "/Volumes/Raid-Renders/ThisShow/ThisShot/"
Thanks for any help. I know there isn't a lot to go on here.
This should do it:
set thisFolder to the POSIX path of (choose file)
set the destinationFolder to (replaceText(thisFolder) as POSIX file as alias)
tell application "Finder" to duplicate (thisFolder as POSIX file as alias) to the destinationFolder with replacing
on replaceText(this_folder)
set AppleScript's text item delimiters to "RENDERS"
set these_items to every text item of this_folder
set AppleScript's text item delimiters to "Raid-Renders"
return these_items as string
end replaceText
As far as I know, you can't assign an AppleScript to a right-click menu. However, you can create a menu bar script. To do this, first save this script as a regular script file in the Scripts folder of the local Library folder.
If you have a little icon in your main menubar (located at the top of the screen) that looks like a scroll (formerly known as the Script Menu), the script should appear somewhere in that menu. If you don't see the icon, run AppleScript Utility (located at /Applications/AppleScript/AppleScript Utility) and check the Show Script Menu in menu bar checkbox.
Now, all you have to do to run the script is open up the Script Menu, find your script, and just click on it once. Questions? Ask. :)

Resources