how to prevent password prompt pops up many times - applescript

My code looks like:
set sourceFolder1 to (path to library folder as text) & "Frameworks:SDL.framework"
set sourceFolder2 to (path to library folder as text) & "Frameworks:SDL_ttf.framework"
set sourceFolder3 to (path to library folder as text) & "Frameworks:SDL_image.framework"
tell application "Finder"
if exists folder sourceFolder1 then
delete folder sourceFolder1
end if
if exists folder sourceFolder2 then
delete folder sourceFolder2
end if
if exists folder sourceFolder3 then
delete folder sourceFolder3
end if
end tell
When I test it, the password prompt pops up 3 times to ask for password. Is there a way to prevent the password prompt pops up more than once? Thanks.
LJ

The Finder is asking for password because you're trying to remove a file from a location that the user doesn't have delete privileges for.
One way to solve this would be to do the deletion via shell commands. Replace delete folder sourcefolder1 with:
do shell script ("rm -rf '" & posix path of sourcefolder1 & "'") ¬
with administrator privileges user name "username" password "password"
Then it won't ask at all. If you want it to ask once, remove everything from user name onward. It will remember the entry for the subsequent commands.
Update: per comment below, the need is to be able to move the item to the Trash rather than delete it outright as rm does. I couldn't think of any way to do it in straight AppleScript, because the administrator prompting is coming from the Finder, and AppleScript (as far as I know) can only perform a privileged action programmatically with the do shell script command. So I wrote a routine that should be equivalent to "Move To Trash" in the Finder:
to deleteItem(thisItem)
set thisItemPath to (POSIX path of thisItem)
set uid to (do shell script "id -u")
if thisItemPath starts with "/Volumes" then
set AppleScript's text item delimiters to "/"
set volName to text item 3 of thisItemPath
set AppleScript's text item delimiters to ""
set targetPath to ¬
("/Volumes/" & volName & "/.Trashes/" & uid)
else
set targetPath to ((POSIX path of (path to home folder)) & ".Trash")
end if
set moveToTrashCmd to ¬
("mkdir -p '" & targetPath & "'; " & ¬
"mv '" & thisItemPath & "' '" & targetPath & "'")
-- set global vars first for userName and password, or omit for prompt
do shell script moveToTrashCmd ¬
with administrator privileges user name userName password userPw
end if
end deleteItem
Here's a sample program that uses the routine:
global fileItems, userName, userPw
on run
set userPw to "password"
set AppleScript's text item delimiters to ":"
set userName to text item -2 of (path to home folder as string)
set AppleScript's text item delimiters to ""
try
fileItems
on error
set fileItems to ¬
(choose folder with prompt ¬
"Select folders to delete:" with multiple selections allowed)
end try
repeat with thisItem in fileItems
deleteItem(thisItem)
end repeat
end run
-- drag 'n' dropped files
on open docs
set fileItems to docs
run
end open

To prevent the password prompt pops up more than once : Use the delete command only once by putting all the folders or files in a list.
Like this:
set frmWKsList to {}
set librF to path to library folder as text
set end of frmWKsList to librF & "Frameworks:SDL.framework"
set end of frmWKsList to librF & "Frameworks:SDL_ttf.framework"
set end of frmWKsList to librF & "Frameworks:SDL_image.framework"
set foldersToDelete to {}
tell application "Finder"
repeat with i in frmWKsList
if exists folder i then set end of foldersToDelete to i as alias
end repeat
delete foldersToDelete
end tell

Related

AppleScript to append date to file

I was recently able to make a drag and drop script in Automator that allowed me to zip and name a file and then automatically apply the date (DDMMYY) but now it's defaulting to (DDMMYYYY) and I can't change it. I've googled for a solution and nothing works since this needs to be at the end of the file name.
Does anyone know what I'm doing wrong or does anyone have an actual script that can help me? Everything I've found only works if the date is at the start of the file name, not at the end (but before the extension).
You can use this AppleScript inside a Run AppleScript action, it renames the archive inserting the date before the file extension
on run {input, parameters}
set thePath to POSIX path of (item 1 of input)
set currentDate to do shell script "date +%d%m%y"
set newPath to text 1 thru -5 of thePath & "_" & currentDate & ".zip"
do shell script "mv " & quoted form of thePath & space & quoted form of newPath
return {POSIX file newPath as alias}
end run
Since you didn't provide any code I can't guess how you name your files, but the way to get DDMMYY is to use shell with the do shell script command:
tell application "Finder"
set theFile to (choose file)
set theName to name of theFile
set name of theFile to theName & "_" & (do shell script "date +%d%m%y") & ".xxx"
end tell
This doesn't get rid of the file extension of the original file. For that to work you would have to use something like this:
tell application "Finder"
set theFile to (choose file)
set theName to name of theFile
set periodIndex to offset of "." in theName
set theName to text 1 thru (periodIndex - 1) of theName
set name of theFile to theName & "_" & (do shell script "date +%d%m%y") & ".xxx"
end tell
The code responsible for removing the file extension comes from this post.
You can add punctuation how you like by putting the desired symbols before the next %. So date +%d.%m.%y is possible and improves readability.

Applescript: Need Help Resolving - Can't Get every file of said folder?

With the help of some users on this form I was able to create this script below which lets you choose a folder then choose which folders within the folder you want to rsync into a OneDrive Backup Folder.
Basically were migrating from a on site network storage to OneDrive for Business and want to create a script that as easy as possible for our users.
The issue I have is I dont want the users to be able to choose the original folder, I want to set the variable up front.
In the script I use:
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?
set OuserName to do shell script "whoami"
set userName to do shell script "echo " & OuserName & " | tr a-z A-Z"
tell application "Finder"
if not (disk userName exists) then
mount volume "SMB Server/" & userName & "$"
end if
delay 2
set theDialogText to "
- Mac H-Drive Migration Tool -
This Application Will Migrate a Copy of Your H Drive Data
to your OneDrive for Buisness Folder Locally on Your Mac
Migration Backup Location:
/Users/" & OuserName & "/OneDrive Folder/H-Drive Migration Backup
** Important **
In the Next Window Please Choose
The Root Folder of Your H Drive
The Drive Label Should Look Like: " & userName & "$"
display dialog theDialogText
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
do shell script "mkdir -p ~/'OneDrive Folder'/'H-Drive Migration Backup'"
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
set AppName to "OneDrive.app"
tell application "Finder" to set Answer_ to exists application file ((path to applications folder as string) & AppName)
if Answer_ is false then
beep
beep
beep
beep
beep
end if
delay 1.5
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
end tell
repeat with thisName in theChosenNames
tell application "Terminal"
do script ("rsync -avpz --delete " & (quoted form of POSIX path of ((theFolder as text) & thisName)) & space & (quoted form of POSIX path of HDriveBackupFolder)
end tell
end repeat
end tell
You asked:
"How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?"
Also, just prior to that, you said:
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
First, lets address setting the value of the theFolder variable. It should be as follows:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
I assume the error is being thrown at the following point in the code:
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
Change set theFolderNames to name of folders of theFolder to:
set theFolderNames to name of folders of container theFolder
To test this, I mounted a volume named MYERSMI5$ at /Volumes/ and created some folders within /Volumes/MYERSMI5$/. Then running the following code:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
tell application "Finder"
activate
set theFolderNames to name of folders of container theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
end tell
It produced a list box, containing the names of the folders I created at that location, to choose from.
I did not try to run the entire block of code you included in your question, so if you're having other issues, you'll need to follow up after making the changes mentioned in my answer. There are good reasons why questions involving debugging code should conform to How to create a Minimal, Complete,and Verifiable example.
You may also want to review the Variables and Properties section in the AppleScript Language Guide.

AppleScript - Change all app icons in a directory

I have folders containing applications. I want to be able to select the folder with an apple script, and have the script go through each app file in that directory, changing the icons for me.
I want to have the icon set from an image stored in the script directory.
I'd really appreciate any help because I've been trying to make this work for a while. This is my progress so far:
property appcurrentCount : 0
on run
set theFolder to (choose folder with prompt "Select the start folder")
doSomethingWith(theFolder)
end run
on doSomethingWith(aFolder)
tell application "Finder"
set subApps to every file of aFolder
repeat with eachFolder in subApps
-- replace icon here somehow
end repeat
end tell
display dialog "Count is now " & appcurrentCount & "."
end doSomethingWith
The script bellow does now what you want. As explained before, your icon file must be type icns. I now add this filter directly in the choose file command.
The selected icon will now replace ALL icons already in each Contents/Resources folder of all applications in the selected folder.
The replacement will be done preserving the name of the icns already in place.
Warning : there is no 'undo' command. your old icons are overwritten !!
set Myicon to choose file with prompt "Select Icns to be copied in every Application of the folder" of type "com.apple.icns"
set MyFolder to choose folder with prompt "Select the folder with all applications to be changed"
set Source to POSIX path of Myicon -- convert path to unix form
tell application "Finder"
set MyApps to every item of MyFolder whose name extension is "app"
display dialog "count apps=" & count of MyApps
repeat with anAps in MyApps -- loop for each App
set IcnFolder to ((anAps as string) & ":Contents:Resources:") as alias
set MyIcns to (every item of IcnFolder whose name extension is "icns")
display dialog "count of icn in " & alaps & " = " & (count of MyIcns)
repeat with oneIcon in MyIcns -- loop for each icns
set Destination to POSIX path of (oneIcon as string)
try
do shell script "cp " & (quoted form of Source) & " " & (quoted form of Destination)
end try
end repeat -- loop for each icns
end repeat -- loop for each App
end tell

How to reveal default Safari downloads folder through Applescript?

I am new to AppleScript and would like to know how to reveal the default downloads folder in Safari through AppleScript.
--- WHAT I HAVE ALREADY TRIED ---
set filePath to do shell script "defaults read com.apple.Safari DownloadsPath"
do shell script "open " & quoted form of filePath
The above script to my understanding will set the variable "filePath" to Safari's default PList entry in preferences.
This works great as long as the location is NOT somewhere within the user home folder. If its within a user folder, the Log shows me a "~/" before the path with no reference to anything prior to the user home folder (relative path)
How do i accomplish my goal? is there a way to get the absolute path to the folder? Or perhaps an alternative method?
Based on #WilliamTFroggard's answer:
tell application "Finder"
set folderPath to do shell script "defaults read com.apple.Safari DownloadsPath"
if folderPath = "~" or folderPath starts with "~/" then ¬
set folderPath to text 1 thru -2 of POSIX path of (path to home folder) & rest of characters of folderPath
open POSIX file folderPath as alias
activate
end tell
The text element is used to strip the trailing / from the home folder (/Users/johndoe/).
The rest property returns every character following the ~ in folderPath (~/Downloads).
/Users/johndoe + /Downloads = /Users/johndoe/Downloads.
You can do this by Applescript GUI Scripting to make Applescript click the 'show downloads' option from the 'view' menu:
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "view"
tell menu "View"
click menu item "Show downloads"
end tell
end tell
end tell
end tell
end tell
Here's a method using Python:
do shell script "python -c \"import os; print os.path.expanduser('`defaults read com.apple.Safari DownloadsPath`')\""
If you really want the long AppleScript method, try this:
set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
set item 1 of filePathWords to (POSIX path of (path to home folder as string))
set filePath to filePathWords as string
else
set filePath to "/" & filePathWords as string
end if
If that second "/" in the path bothers you (it sort of bothers me...), you can use this instead:
set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
set item 1 of filePathWords to (do shell script "cd ~ && pwd")
set filePath to filePathWords as string
else
set filePath to "/" & filePathWords as string
end if
I think what you want is this:
tell application "Finder"
activate
open ("/Users/yourname/Downloads" as POSIX file)
end tell
Just replace "yourname" with your name in Finder.

Applescript System Events Returns .DS_Store - How can I ignore

I have looked around on the net for hours looking for this answer so I apologize if its there somewhere. This script below works fine except for the fact that it returns the .DS_Store file how can I exclude it from this query and all other hidden files for that matter but because I am creating the folder in my script the only one there is .DS_Store
tell application "System Events"
set listOfInputs to POSIX path of every disk item of folder a as list
end tell
Here is the full script below. I have been dropping files into the in folder after it is created but will eventually prompt for files before it is created.
on run
tell application "Finder"
if not (exists folder "IN") then
make new folder at desktop with properties {name:"IN"}
end if
if not (exists folder "OUT") then
make new folder at desktop with properties {name:"OUT"}
end if
end tell
set theINfolder to path to the desktop as string
set a to theINfolder & "IN:"
tell application "System Events"
set listOfInputs to POSIX path of every disk item of folder a as list
end tell
set inputs to listOfInputs
set theOutfolder to path to the desktop as string
set outputFolder to theOutfolder & "OUT:"
set params to {}
main(inputs, outputFolder, params)
end run
The following will work:
set listOfInputs to list folder a without invisibles
But without invisibles cannot be combined with POSIX path, so we use a loop instead:
set listOfInputs to {}
tell application "System Events"
set the_items to list folder a without invisibles
repeat with i from 1 to the count of the_items
set this_item to alias ((a as Unicode text) & (item i of the_items))
set end of listOfInputs to POSIX path of this_item
end repeat
end tell
The List Folder command has been deprecated and may stop working unexpectedly.
Try:
set inFolder to (path to desktop as text) & "IN"
do shell script "mkdir -p " & quoted form of (POSIX path of inFolder)
set listOfInputs to paragraphs 2 thru -1 of (do shell script ("find " & quoted form of (POSIX path of inFolder) & " \\! -name \".*\""))
You can get the result as a string separated by spaces like this:
set inFolder to (path to desktop as text) & "IN"
do shell script "mkdir -p " & quoted form of (POSIX path of inFolder)
set {TID, text item delimiters} to {text item delimiters, " "}
set listOfInputs to paragraphs 2 thru -1 of (do shell script ("find " & quoted form of (POSIX path of inFolder) & " \\! -name \".*\"")) as text
set text item delimiters to TID

Resources