Granting permissions to AppleScript script - applescript

I've a simple AppleScript that it's supposed to delete everything in the Downloads folder with exception of selected list of folders passed at the end.
tell application "Finder"
delete (every item of "Downloads" of folder currentUser of folder "Users" of startup disk whose name is not in {"PreciousFolder"})
end tell
On save the AppleScript returns the following error:

You are going to delete every item of the literal string "Downloads" which makes no sense. Add the folder specifier
tell application "Finder"
delete (every item of folder "Downloads" of folder currentUser of folder "Users" of startup disk whose name is not in {"PreciousFolder"})
end tell
or shorter
tell application "Finder"
delete (every item of (path to downloads folder) whose name is not in {"PreciousFolder"})
end tell

Related

Unable to find the application support folder for a specified application inside an Apple Script

I have an apple script that asks the user to select an application as a prompt. I would like to know how it could be possible to find the directory of the Application Support folder for this application per example, Spotify is
~/Library/Application Support/Spotify/
On my mac, but like is there a way inside apple script to find this directory for the specific app. It would really be appreciated, thanks!
Because I would like to then delete this folder using Apple Script
This should accomplish what you're looking to achieve. Be careful though because using System Events to delete... deletes the item permanently instead of sending it to the Trash
property folderNames : missing value
tell application "System Events"
set appSupportFolder to application support folder
set folderNames to name of folders of appSupportFolder
set theChoice to my chooseFolderToDelete()
delete folder theChoice of appSupportFolder
end tell
to chooseFolderToDelete()
activate
set theChoice to (choose from list folderNames with prompt ¬
"Select an Application's Support folder to delete" OK button name ¬
"Delete Folder" cancel button name "Cancel") as text
end chooseFolderToDelete

Non-recursive folder actions with applescript

I have a Folder Action watching a folder to run an Applescript whenever a file is added to it.
The Applescript tells my thumbnail generator app to process the images. This works fine. The app creates a subfolder, and places the thumbnails inside.
The issue is that the Folder Action gets triggered for each image made in the subfolders, and it makes a thumbnail of the thumbnails in yet another subfolder.
This happens only once for some reason, but its still too much.
Is there a way I can set a Folder Action to ignore files added to its subfolders? I need to maintain this directory structure where the thumbnail generator outputs into a child folder.
Here's the script:
on adding folder items to this_folder after receiving added_items
repeat with eachItem in added_items
tell application "ThumbsUp" to open eachItem
end repeat
end adding folder items to
On another forum someone suggested this instead:
on adding folder items to this_folder after receiving added_items
repeat with eachItem in added_items
tell application "Finder" to set itsParent to (container of eachItem) as alias
if itsParent is this_folder then tell application "ThumbsUp" to open eachItem
end repeat
end adding folder items to
which should make ThumbsUp not run on items in the subfolder, but for some reason it continues with the same behaviour.
How can I stop folder action from running on subfolders?
Someone on another forum posted script as an answer, which works.
The problem is because ThumbsUp creates the subfolder, and that subfolder gets added to the list of files to open. So this script fixes that
on adding folder items to this_folder after receiving added_items
repeat with eachItem in added_items
tell application "System Events" to set itsKind to kind of eachItem
if not (itsKind is "Folder") then tell application "ThumbsUp" to open eachItem
end repeat
end adding folder items to
Folder Actions do not normally recurse, so I suspect the problem is that the "ThumbsUp" application is designed to process folders of images as well as individual images. When it creates a folder to store the thumbnails, that folder is seen as 'added' to the watched folder, and is sent to the script and back to ThumbsUp. Open ThumbsUp and see if you can set the output folder to a different location. If that doesn't work, try excluding folders from being processed in your script:
on adding folder items to this_folder after receiving added_items
tell application "System Events"
repeat with an_item in added_items
set disk_item to disk item (POSIX path of an_item)
if class of disk_item is not folder then
tell application "ThumbsUp" to open disk_item
end if
end repeat
end tell
end adding folder items to

Listing folder in /Users

Using Applescript, I have been able to successfully list the folders on my desktop as a selection using this code:
set the_folder to (path to desktop)
tell application "Finder"
set foldernames to name of every folder of entire contents of the_folder
end tell
set theChosenOne to choose from list folder names
However, when I attempt to do the same for the /Users folder:
set Users to "/Users"
set the_folder to Users
tell application "Finder"
set foldernames to name of every folder of entire contents of the_folder
end tell
set theChosenOne to choose from list foldernames
It returns this error: error "Can’t get entire contents of \"/Users\"." number -1728 from «class ects» of "/Users"
Searched on that error, but not finding much info. Thanks for any help you can give me with this one.
The error occurs because the Finder does not support slash separated POSIX paths.
But there is a simpler solution. path to users folder return an alias reference to the folder /Users which can be used directly.
set the_folder to path to users folder
tell application "Finder"
set foldernames to name of every folder of entire contents of the_folder
end tell
set theChosenOne to choose from list foldernames
Caveat: Be aware that entire contents is very slow. After 2 minutes you will get an Apple Event timed out error. You might wrap the Finder tell block in a with timeout block. However I'd recommend find or mdfind of the shell which are incredibly much faster. And most likely you will get also an access privileges violation error.

How do I Copy files to a USB with apple script that runs on said USB?

I run a computer lab at Denver University. We are having an issue with our backup system, and I need to copy/duplicate all of the files on the (Apple) computers onto a USB that I keep at my desk.
At the moment, I need to plug in a USB into each of the computers and manually duplicate each of the items of the "documents" folder and deposit them into a new folder on my USB. I need an Apple Script that I put on my USB, insert my USB into a computer, activate the app, and it will make a new directory named "(User name) upload", and deposit all of the items into that directory. Here is what I have so far:
tell application "Finder"
set theFolder to disk / Volumes / Lexar / stuff
set Files1 to Users / matanya / documents
tell application "Finder"
try
duplicate file Files1 to theFolder
on error
tell application "Finder"
display dialog "Transfer Failed"
end tell
end try
end tell
one of the issues is that every time I run the script, I get an error that says the the variable "Volumes" is not defined. Another one is that I am afraid that when I plug this script into another computer, it will not find the folder "matanya" that I have in my directory. Is there a way to call it "home" or something?
First of all the Finder accepts only HFS paths – starting with a disk name and colon separated. The folder Volumes doesn't matter when using HFS paths.
path to documents folder points always to the folder Documents of the current user.
set theFolder to "Lexar:stuff:"
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
try
duplicate documentsFolder to folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
More sophisticated this version creates a folder with the short user name of the current user and duplicates the documents folder to that individual folder
set theFolder to "Lexar:stuff:"
set currentUser to short user name of (system info)
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
if not (exists folder currentUser of folder theFolder) then
make new folder at folder theFolder with properties {name:currentUser}
end if
try
duplicate documentsFolder to folder currentUser of folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
With launchd you even could run this script automatically whenever the USB drive is inserted.

How do I use applescript to name a folder by pasting from the clipboard

I work in graphic design and want to create an script for indesign that will automatically:
open a specified document template.
Create a new project folder.
Name that folder with text from the clipboard.
Save the newly opened document to the newly created folder and name that document with the same text from the clipboard.
I am very new to Applescript so type very slowly and use small words so that I can understand.
Here is what I have come up with in the way of code so far:
tell application "Adobe InDesign CS5"
set myDocument to open "cm:Graphic_Design:Design Studio Templates:Brochure_042012_001:Brochure_042012_001.indt"
end tell
tell application "Finder"
make new folder at folder "Work" of folder "Graphic_Design" of disk "cm" with properties {name:"untitled folder"}
set name of folder "untitled folder" of folder "Work" of folder "Graphic_Design" of disk "cm" to pbpaste
end tell
Try:
tell application "Finder" to set newFolder to make new folder at "cm:Graphic_Design:Work" with properties {name:(the clipboard as text)}
tell application "Adobe InDesign CS5"
tell active document to save saving in (newFolder as text)
end tell

Resources