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.
Related
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.
I totally forgot about Xcode AppleScript Application.
Basically, the application will read value from plist file.
My applescript to read this variable is
set the plistfile_path to "~/Desktop/example.plist"
tell application "System Events"
set p_list to property list file (plistfile_path)
-- read the plist data
set theNameFromPlist to value of property list item "theName" of p_list
set theEmailFromPlist to value of property list item "theEmail" of p_list
set thecreationDateFromPlist to value of property list item "creationDate" of p_list
end tell
now how do I integrate this to Xcode?
Should I go on AppDelegate.applescript and add wherever after "applicationWillFinishLaunching_(aNotification)" this script ?
followed by property NametextField : theNameFromPlist
I'm probably totally wrong here, just can't think.
PS swift will do it if any easier
Like vadian said in the comments, In Xcode, System Events won't run properly so you can just use the native Cocoa classes but I prefer to
use "do shell script" applescript/asobjc command and run the "defaults" command so here is a way to do that when the application will finish launching:
on applicationWillFinishLaunching_(aNotification)
set plistFile to "~/Desktop/example.plist"
set theNameFromPlist to do shell script "defaults read " & plistFile & " theName"
set theEmailFromPlist to do shell script "defaults read " & plistFile & " theEmail"
set theCreationDateFromPlist to do shell script "defaults read " & plistFile & " creationDate"
end applicationWillFinishLaunching_
And if you are trying to display the name in a TextField on a Window
in the Interface Builder you can just add Modify the code to this:
property nameTextField : missing value
on applicationWillFinishLaunching_(aNotification)
set plistFile to "~/Desktop/example.plist"
set theNameFromPlist to do shell script "defaults read " & plistFile & " theName"
set theEmailFromPlist to do shell script "defaults read " & plistFile & " theEmail"
set theCreationDateFromPlist to do shell script "defaults read " & plistFile & " creationDate"
tell nameTextField to setStringValue_(theNameFromPlist)
end applicationWillFinishLaunching_
(make sure to connect the property nameTextField to a TextField in the Interface Builder)
hope this Helped!! :D
Mentioning Cocoa classes I actually meant this
set plistFile to POSIX path of (path to desktop) & "example.plist"
set thePlist to (current application's NSDictionary's dictionaryWithContentsOfFile:plistFile) as record
set theNameFromPlist to thePlist's theName
set theEmailFromPlist to thePlist's theEmail
To add new key value pairs and write the plist back to disk add
set thePlist to thePlist & {stringKey:"Foo", booleanKey:false}
set cocoaDictionary to current application's NSDictionary's dictionaryWithDictionary:thePlist
cocoaDictionary's writeToFile:plistFile atomically:true
I am packaging an application into a .app directory for "drag install" or whatever it's called and I have a weird iessue with file association.
I set my application as a viewer for .xyz files, and the system does start my app when I double click that file; the only problem is that the path of the file I clicked is nowhere in the args[], there's only one parameter that is something like ~psn_0_901340 and I think is a timestamp because it changes every time.
So... what am I supposed to do? I've been sitting here for 2 hours straight and can't find a solution.
I think what you want is an AppleScript droplet.
A shortened version of the AppleScript from that link:
on open dropped_files
set the_command to quoted form of POSIX path of (path to resource "script.sh")
set file_list to ""
repeat with file_path in dropped_files
set file_list to file_list & " " & quoted form of POSIX path of file_path
end repeat
set the_command to the_command & file_list
do shell script the_command
end open
Export as an application using Script Editor. Place script.sh in the Resources folder.
Add your file extension associations to Info.plist. You may need to launch or move the droplet before OS X notices the change & allows you to double-click files.
If you want to launch Terminal or capture the script output, see the full AppleScript.
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
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