AppleScript cannot remove files from /Library - applescript

I write an AppleScript:
to check if two specific dictionaries (File1 and File2) files exit in /Library/Dictionaries/
if either of the two files exits, then completely delete that file and the other
The script works fine in AppleScript Editor. I then save the script as an app, test it, and the app also works just fine. I mean both the script in AppleScript Editor and the saved app detect and remove both File1 and File2 from /Library/Dictionaries/.
However in PackageMaker Post Action, when called, the app removes neither File1 nor File2 although it detects them and even shows the dialog message (see code line below).
Here is the code:
tell application "System Events"
if exists (application process "Dictionary") then
tell application "Dictionary" to quit
end if
end tell
try
set theOtherFile1 to POSIX file "/Library/Dictionaries/File1.dictionary"
set theOtherFile2 to POSIX file "/Library/Dictionaries/File2.dictionary"
tell application "Finder"
if exists file theOtherFile1 then
display dialog "File1/File2 exits. Do you want to remove it?" with title "Note" buttons {"No", "Yes"} default button "Yes"
if the button returned of the result is "No" then
delay 2
else
do shell script "rm -R /Library/Dictionaries/File1.dictionary" with administrator privileges
do shell script "rm -R /Library/Dictionaries/File2.dictionary" with administrator privileges
end if
end if
end tell
end try
delay 5
tell application "Dictionary" to activate

Try this:
tell application "System Events"
if exists (application process "Dictionary") then
tell application "Dictionary" to quit
end if
end tell
try
set theOtherFile1 to POSIX file "/Library/Dictionaries/File1.dictionary"
set theOtherFile2 to POSIX file "/Library/Dictionaries/File2.dictionary"
tell application "Terminal" to activate
tell application "System Events"
keystroke ("sudo chmod 777 " & theOtherFile1) as string
display dialog "Click the button after you've typed the root password into Terminal." buttons {"OK"}
keystroke return
keystroke ("chmod 777 " & theOtherFile2) as string
display dialog "Click the button after you've typed the root password into Terminal." buttons {"OK"}
end tell
do shell script "rm -rf /Library/Dictionaries/File1.dictionary"
do shell script "rm -rf /Library/Dictionaries/File2.dictionary"
This should do the trick.
I'm using a PC at the moment, otherwise I'd check first, but I think this will run correctly.

Related

Run emacs using applescript and open file

What I would like to do is have an automator script open a file in emacs when I drag a file onto it, and bring terminal to the front.
Right now, when I try to do this with input set to ~/Desktop/test.txt, it either opens up the main page, or it ends up with ~/Desktop/testtxt. Am I doing this wrong? Is there a better way to do this?
This is what I have right now:
set thePath to POSIX path of input
tell application "Terminal" to do script "emacs"
tell application "System Events"
tell process "Terminal"
activate
delay 2
keystroke "x" using control down
delay 1
keystroke "f" using control down
delay 1
keystroke thePath
delay 1
keystroke return
delay 1
end tell
end tell
return input
end run
Use the file path as an argument to emacs
on run {input}
tell application "Terminal"
repeat with i in input
do script "emacs " & quoted form of POSIX path of i
end repeat
activate
end tell
return input
end run

Applescript to open terminal, run command, and show - Not working

I´m trying to create a keyshortcut to open terminal in current folder. Looking around, I found this code to create a service (the part of adding the shortcut to this service is solved), only added things are the "; clear" and some of the "activate" so it shows
on run {input, parameters}
tell application "Finder"
activate
set myWin to window 1
set theWin to (quoted form of POSIX path of (target of myWin as alias))
tell application "Terminal"
activate
tell window 1
activate
do script "cd " & theWin & ";clear"
end tell
end tell
end tell
return input
end run
It is not working as i would like.
troubles:
it opens two windows in terminal, have no idea why. It has nothing to
do with the added "activate"… it has always donde that
if I select an item on finder ( a folder ) it opens its parent directory and i would
like it to open the selected folder
this is my very first try with Applescript so if the error is obvious i just can't see it
Thanks in advance
The do script command already opens a window in Terminal. Try it this way:
tell application "Finder" to set theSel to selection
tell application "Terminal"
set theFol to POSIX path of ((item 1 of theSel) as text)
if (count of windows) is not 0 then
do script "cd " & quoted form of theFol & ";clear" in window 1
else
do script "cd " & quoted form of theFol & ";clear"
end if
activate
end tell
I like the reopen approach better...
tell application "Finder" to set currentFolder to target of front Finder window as text
set theWin to currentFolder's POSIX path
tell application "Terminal"
if not (exists window 1) then reopen
activate
do script "cd " & quoted form of theWin & ";clear" in window 1
end tell

applescript to close pdf window

I have an applescript that looks like this:
repeat
tell application "Adobe Reader"
open "filepath/name.pdf"
end tell
delay (60)
tell application "Adobe Reader"
open "filepath/name1.pdf"
end tell
delay (60)
tell application "Adobe Reader"
open "filepath/name2.pdf"
end tell
delay (60)
end repeat
I want to be able to close pdf windows after they have been opened. The issue is that these pdfs reside on a share and users have the ability to update them. The script will only display the updated pdf if it is stopped and restarted. I do not want to have to do this manually. How can I do this?
Here's a solution, which makes Preview scriptable and then continues to open and close a file of your choice.
-- http://www.macworld.com/article/1053391/previewscript.html
on addAppleScriptFeatures()
try
tell application "Finder"
set the Preview_app to (application file id "com.apple.Preview") as alias
end tell
set the plist_filepath to the quoted form of ¬
((POSIX path of the Preview_app) & "Contents/Info")
do shell script "defaults write " & the plist_filepath & space ¬
& "NSAppleScriptEnabled -bool YES" with administrator privileges
do shell script "chmod -R 755 " & the quoted form of (POSIX path of the Preview_app) with administrator privileges
return true
on error myErr number MyNr
display dialog myErr & " (" & MyNr & ")." buttons {"OK"} default button "OK" with icon 0
return false
end try
end addAppleScriptFeatures
if addAppleScriptFeatures() then
set f to choose file
tell application "Preview"
activate
open f
delay 5 -- short for testing
close window 2
end tell
end if

Applescript to Expose all Finder Windows?

I'm trying to figure out how to write an Applescript that will Exposé all Finder Windows.
Application Logic
I think the script needs to have this application logic:
Check a residual setting and get the name of the last "Frontmost
Application" (perhaps use a text file ~/last-application.txt to store this?)
Grab the name of the current Frontmost Application
If the name of the current of the Frontmost Application is Expose, then activate the previous frontmost application
Else, activate finder, and then activate expose for just finder windows
Desired Behavior
When the script is activated all the finder windows (and only the finder windows) will be shown in Exposé
If the script is then run again (and no finder window was selected) the script will just switch back to the last frontmost application
I'm not sure how to get this working though. If there is another utility that does this automatically that'd be great, too.
set f to "/s/x/finderexpose"
set prev to do shell script "touch " & f & "; cat " & f
if prev is not "" then
delay 0.5 -- time to release modifier keys used in a shortcut
tell application "System Events" to key code 53 -- esc, if Exposé is open
delay 0.3 -- for the Exposé animation?
activate application prev
do shell script "echo '' > " & f
else
do shell script "echo " & quoted form of (path to frontmost application as text) & " > " & f
activate application "Finder"
delay 0.05
tell application "System Events" to key code 125 using {control down} -- ⌃↓
end if
It'd be less ugly if the part for switching to the previous application was left out:
activate application "Finder"
delay 0.05
tell application "System Events" to key code 125 using {control down}

Using AppleScript to choose a file in Safari

I am trying to write some automation code (primarily in Ruby Selenium). At some point, a file chooser is opened in Safari so that the user can select a file for upload. Selenium cannot handle this, but I think AppleScript should be able to. I am new to AppleScript and haven't been able to find any boilerplate code of someone automating a file chooser dialog. I'm reading through the AppleScript docs, but any ideas would be most helpful.
Some more searching and I found a great answer here: Applescript file dialog with UI scripting
Here's what I ended up using:
on run argv
tell application "Safari"
activate
-- Usage check
set argc to count argv
if argc is not greater than 0 then
return "Usage: SafariFileChooser file_name [window_name]"
end if
-- The file we will choose to open
set file_name to item 1 of argv
-- Flip to the named window, if specified
if argc is equal to 2 then
set window_name to item 2 of argv
set flip_count to index of window window_name
repeat (flip_count - 1) times
activate
tell application "System Events" to keystroke "`" using command down
end repeat
end if
-- Interact with the dialog using System Events (thanks mcgrailm)
tell front window
activate
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke file_name
delay 1
keystroke return
delay 1
keystroke return
end tell
end tell
end tell
return 0
end run
Another option I just discovered is to specify the directory using the command-line:
do shell script "defaults write com.apple.Safari NSNavLastRootDirectory /path/to/directory"
This way you can do slightly less in UI scripting. Run this command before you open the file chooser, and it will put you into the directory specified. Include all the files you need in this directory, and you can just script command+a to select them all, and return.

Resources