I want to run an applescript when a specific folder of mine is opened. Then, depending on the input, close the folder, or leave it open. All that without opening the folder. So, basically:
1) Try to open folder
2) Folder doesn't open, but window pops up
3-A) If user clicks ok folder opens
3-B) If user clicks cancel, it just exits out of script, leaving the folder unopened.
How can I do this? Remember: The folder CAN'T open in the background, it can ONLY open if the user presses OK. Help?
For this task, you'll want to use Folder Actions, which trigger script files. Below are the events involving a folder that could trigger a script, and the special corresponding handlers.
A folder is opened — on opening folder window for this_folder
A folder is closed — on closing folder window for this_folder
One or more items are added to a folder on adding folder items to this_folder after receiving these_items
One or more items are removed from a folder — on removing folder items from this_folder after losing these_itmes
A folder window is moved — on moving folder window for this_folder
The first bullet is what you'll be focusing on in this case. As the bullet implies, the script is triggered only when the folder actually opens. However, you can hack your way around this issue simply by adding this line at the very beginning of your script:
tell application "Finder" to close first window whose target is this_folder
And here is the full script:
on opening folder window for this_folder
tell application "Finder"
close first window whose target is this_folder
-- the window may appear briefly, but at least you've accomplished your goal
display dialog "Open folder " quoted form of the name of this_folder & "?" buttons{"Yes,"No"} default button 1 cancel button 2
-- pressing the "No" button is exactly the same as pressing the "Cancel" button on a regular dialog, and the script terminates
make new Finder window with properties {target:this_folder}
end tell
end opening folder window
SAVE YOUR SCRIPT FILE IN THE FOLDER ACTIONS FOLDER OF YOUR LOCAL SCRIPTS FOLDER. Create the folder yourself if it doesn't already exist. Doing so will allow your script to even run. Now, for this to function properly, you will need to attach the saved script file—not an application/application bundle—to your desired folder. To do this...
Perform a right-click on your desired folder
Click the menu item "Folder Actions Setup" at the bottom
Locate your saved script in the dialog that appears
Click "Attach"
Close the "Folder Actions Setup" window
Once you've done this, your script is ready to go.
Addendum: If your folder contains confidential information, beware that, as stated by user57368, "it is probably not possible to make the system even remotely secure with AppleScript."
Instead of an ordinary folder, you want an application bundle containing a folder or disk image. If you use an encrypted disk image, it will be more difficult for the user to access the contents without following your preferred procedure, but beware that it is probably not possible to make the system even remotely secure when you're using AppleScript.
If you just want to present a license agreement or other static notice before allowing the user to read the data, just read the hdiutil man page for how to create a disk image that presents a license agreement upon mounting.
Related
What I ultimately want to do is have a .app that I can open DMG files with. When the DMG file is opened the app, it opens the DMG in a new Finder tab and then shows the Toolbar in Finder after half a second. AppleScript for the later would look like this:
delay 0.5
tell application "Finder"
tell the front window to set toolbar visible to true
end tell
I tried looking up how to open a DMG file in a new Finder Tab with AppleScript but didn't find a solution. If I end up not finding one, then I'll see if I can do so with a Terminal command and then do it in AS with do shell script. For the file input, I tested this code based off what I found here but it doesn't work, it just creates a copy of the DMG:
on run {input, parameters}
if input is {} then
set inputFile1 to ¬
quoted form of POSIX path of ¬
(choose file with prompt "Please select a file to process:")
else
set inputFile1 to quoted form of ¬
(POSIX path of first item of input)
end if
display dialog inputFile1
end run
I did a bunch of googling as well for how to do so but I found nothing. What I can do to end up with an AppleScript that does everything I described?
I would open the dmg with hdiutil which seems to open the image into a standard window:
set runDMG to quoted form of POSIX path of (choose file)
do shell script "/usr/bin/hdiutil attach " & runDMG
Basically, you get the posix path of your dmg file and feed it to hdiutil attach. The disk image will open and —for me at least— consistently opens in a default finder window (i.e. same size as other windows, list view, sidebar visible, toolbar visible).
I should add that typically when I double-click on a dmg, it opens in a 'drag to applications folder' window which is icon view and shows only the app and the applications folder.
Separately, this code seems to work with bringing the disk image's folder to the front window.
tell application "Finder"
parent of startup disk
--> computer container of application "Finder"
open computer container
set target of window 1 to (last item of window 1 as alias)
--> disk "Key Codes" of application "Finder"
end tell
There are (at least on my Sierra mac), three items in the 'computer' folder: 'Network', 'Remote Disc', the startup volume. When you open a dmg, the virtual appears here as a 'removable'. I've found that when you get a list of folders in the computer container, the removable is the last item in the list. I don't know if that ever changes, and I haven't played around with multiple disk images simultaneously but it provides a starting point for specifying the desired window if for some reason, it doesn't open automatically.
I am trying to use Automator to take a bunch of previously-created folders and create two new folders within each one, an "Originals" folder and a "Scans" folder. I would like to be able to drag or select the folders that need the subfolders and then have the action run automatically.
I've tried making both an action and a service to add the subfolders, but when I select multiple folders, it only runs once.
Here is a screenshot of what I've done so far...
Try saving the workflow as an Application:
Save the Application to your Desktop, then you can drop folders there whenever you want.
Here is an AppleScript that, when Exported as an Application, functions as a Droplet to make the desired folders in every folder dropped thereon:
on open droppings
repeat with everyDrop in droppings
if (info for everyDrop)'s folder is true then
--make two folders
tell application "Finder"
make new folder at (everyDrop as text) with properties {name:"Originals"}
make new folder at (everyDrop as text) with properties {name:"Scans"}
end tell
end if
end repeat
end open
on run
display dialog "Drop some folders here to add Originals and Scans folders" buttons {"Aye Aye"} default button "Aye Aye"
end run
Super simple question, but I can't fond the answer.
I want the Finder to open mounted disk when I plug in USB drive... How?
Thank you #mark-setchell
I've ended up with this Apple Script for my /Volumes folder:
on adding folder items to this_folder after receiving added_items
try
tell application "Finder"
--open window and activate finder
activate
--open the USB
open added_items
end tell
end try
end adding folder items to
So every time some new item appears in /Volumes folder Finder opens window and selects this item.
This solution has some delay before opening finder (5-10 sec). I guess Folder Actions just work this way...
You should be able to do this by adding a Folder Action to the folder Volumes which is where external disks get mounted. AFAIK, you can only do that in the Finder and the /Volumes folder is set invisible in the Finder - thanks, Apple.
So, first make the /Volumes folder visible to the Finder, by starting the Terminal and running
sudo SetFile -a v /Volumes
Now, start the Finder and select your System drive and then Macintosh HD on the left pane. You should now see
Applications
Library
System
User
Volumes
Right-click on the Volumes folder and go to Setup Folder Actions and attach the add - new item script. You can then diddle around with that to do whatever you want the Finder to do when a disk is inserted.
Adding to #MarkSetchell's answer, to make the /Volumes folder visible I had to use sudo chflags nohidden /Volumes instead. However an easier way to make this folder visible (and Temporarily, which might be preferred) is with a key combination:
Cmd (⌘) + Shift + .
(also my context menu item is Services > Folder Actions Setup)
And to save others the pain, the folder these scripts are saved in is /Library/Scripts/Folder Action Scripts (I'm ashamed to say it took me a few hours to find it)
Create a new file there using the terminal and sudo, paste the script above, and add it in folder actions setup. It works like a charm (for me at least).
I need to rename image files sequentially as they are added to a folder. ie. image-0001.jpg and image-0002.jpg are in a folder I add test.jpg and it is renamed image-0003.jpg. I have tried automators rename function but it will start over with image-0001.jpg each time a new file is added instead of continuing the sequence.
Any help is greatly appreciated.
You could make this easy on yourself by using a handy application built into the OS called "Folder Actions". Folder Actions contains one or more special handlers, formally known as folder action event handlers, that run when they are triggered by a change in the target folder. I know I'm confusing but I'll do my best.
What you are trying to accomplish requires an adding folder items to event handler. It requires one direct parameter, which can be anything you wish i.e. target_folder. The handler requires an additional parameter as well; after receiving, which should also be a variable name, i.e. these_items. I have composed a script for you that should do the trick. I have added comments that show you what I'm doing when I do it. Here it is:
on adding folder items to the target_folder after receiving these_items
tell application "Finder"
set all_images to every item of the target_folder as list
repeat with i from 1 to the count of these_items --iterates through all the items you dropped in the folder
set this_image to item i of these_items --the current image
set the name of this_image to "image" & (i + the count of all_images) as string --renames the image based on the number of images already in the folder
end repeat
end tell
end adding folder items to
YAY! The script is done! But are WE done? Not quite. We still need to attach the script to a folder (the script won't run if you try to execute it in script editor).
To do this, first save the script as a Script File in the Folder Action Scripts folder in the Scripts folder in either the local Library folder or the current user's Library folder. Create the folder yourself if it doesn't already exist. Next, launch the Folder Actions Setup application by double-clicking it in the AppleScript folder in the Applications folder. In the window that comes up, click the + button under the table on the left (click the "Enable Folder Actions" checkbox if it isn't already checked) to open a standard file browser sheet, navigate to your desired folder, and click "Open". The Choose a Script to Attach sheet automatically opens, listing all the scripts in all of the Folder Action Script folders. Choose the newly-created script, click "Attach", and BAM you are done!
To see the script in action, drag an image onto the folder. The image is instantly renamed, regardless of if the folder window is open. If you have any questions, or if the script doesn't work, just ask me. :)
well without digging through some code and handin you an answer I'll tell you want you want to do
is create a while loop that checks for the existence of image-000 & i where i is a variable of course and if it exists increment i then when the file doesn't exist rename your file.
Can I make an apple script that auto runs when I put in my flash drive? I want to be able to do this so that when I put my flash drive in the computer at school I can make my presentation automatically play to save time and so i don't have to go through all my files in front of the class. We use macs at school and I have a mac.
There's no way using plain AppleScript to receive events when a drive is plugged in.
What you could do is create a poll timer that checks for the drive at a specified interval:
repeat
set driveName to "YOURDRIVENAME"
set driveExists to (do shell script "ls /Volumes | grep " & driveName)
if driveExists contains driveName then
-- do whatever
end if
delay 5
end repeat
I wrote that off the top of my head, and I haven't tested it, but something along those lines should work. delay 5 tells the script to wait 5 seconds before polling again, change this to suit your needs. I haven't tried anything like this with AppleScript before so it may be taxing on resources.
You can activate a Folder Action applescript to watch for newly attached volumes.
Duplicate the script /Library/Scripts/Folder Action Scripts/add - new item alert.scpt and modify the copy to open your presentation or what-have-you.
Activate the script via /Library/Scripts/Folder Actions/Configure Folder Actions (a link to /System/Library/CoreServices/Folder Actions Setup.app):
Launch Configure Folder Actions and enable it with the top check box.
Click the left plus sign to add a folder to watch.
Hit ⇧g (command-shift-g) to navigate to an invisible folder. Type: /Volumes and hit enter
Hit Enter or click the Open button without selecting anything to attach to the /Volumes directory itself.
Choose your modified add - new item alert.scpt from the Attach pane.