Detect USB pendrive plugging in with applescript - applescript

I want to execute automator script when user plug in a pendrive with specific name.
How can I do it?

Applescript and automator cannot do this. However launchd can. Launchd can watch folders, and if something changes in the folder then launched will run your command. In your case you would want to watch the /Volumes folder. When you mount the USB drive it's mount point will be that folder. Therefore you need to write yourself a launchd plist file, and your command could be to run your automator script. Just google for more info on launchd. I did a quick search and this link gives most of the information you need.

Unless I'm missing something, I believe that Applescript can in fact do what you want. A Folder Action listening to the Volumes directory (as per Auto-Detect Mounted Volumes) will run any time a volume is mounted. You can then run a shell script (or some other method) that detects if the mounted drive is your drive, and run your workflow if so (or perhaps more easily, exit if not).

Related

Permissions problem from running rsync through a keyboard shortcut

I have an Automator 'Quick Action' which simply runs a shell script (using bash) which comprises of a single rsync command. I've assigned a keyboard shortcut to this from System Preferences > Keyboard > Shortcuts.
Since updating to Catalina, I'm getting a permissions problem like shown in the second last comment here: https://developer.apple.com/forums/thread/126497.
Basically, I can run my rsync command in Terminal and it'll work fine. I can also open the Automator file and run it from there and it'll work fine. But running it from a keyboard shortcut throws the permissions error.
I've granted full disk access to both rsync and my quick action file, but to no effect. The last comment in that link mentions that you need to also grant full disk access to the program that calls your program (in their case it was cron), but I don't know what the calling program is in my case.

Execute script in new terminal window on hard drive plugging

I would like to execute a backup script with rsync everytime a specific hard drive is plugged to my computer on ubuntu 16.04.
However I would like the user to be prompted if the backup should run or not and then he should be able to see the rsync output live in a terminal.
What I did so far:
I created a rule at /etc/udev/rules.d/backup_on_mount.rules
ACTION=="add", ATTRS{idVendor}=="1058", ATTRS{idProduct}=="0820", RUN+="/path/to/my/script/backup_on_mount.sh"
I created the backup_on_mount.sh script and made it executable
However I did not succeed in popping a new terminal window with my script running in it when I plug my hard drive.
Does anybody have an idea on how to begin ?
Thank you
You are not supposed to run anything that long from udev rules directly. One way of dissociating your long running script from the udev rule is by scheduling it (with at -M now command for example) instead of running it directly. The proper way of finding the right DISPLAY to open the new terminal depends on your desktop environment

Mute Macbook pro when computer is shutting down

I'm thinking of using the Mac's applescript to make a program that mutes the system when it is shutting down.
Though I'm new to applescript and I don't know how to use the IF-statement to determine if the system is shutting down. I've done some googling and I've found that the finder app is the app that is "controlling" the shutdown, but i don't know how to check if the state is "shut down". Can anybody assist me in this matter?
AppleScript has no direct mechanism for detecting a shutdown/logout.
It does have a mechanism for creating applications that can react to themselves being quit.
Thus, you can:
use AppleScript to create a stay-open application (.app bundle) with a standard on quit handler, in which you perform the desired action (
make sure that the application is launched on login - in the simpler case as a Login Item (via System Preferences, see below), or, with more flexibility but complexity, as a launch agent (see https://stackoverflow.com/a/22872222/45375).
Instructions:
Open Script Editor and open a new script window.
Paste the following code:
# This standard handler is called when the application quits.
on quit
# Mute the system volume.
# !! See caveat below.
set volume with output muted
continue quit # signal to the system that it's OK to quit
end quit
Save the script as a stay-open application:
with File Format Application
check Stay open after run handler
Open System Preferences > Users & Groups > Login Items, drag the newly saved *.app bundle into the list, and check the checkbox next to it, so as to make it launch hidden.
The final step is to hide the new application's Dock icon, as there's no reason for it to have one:
From Terminal, run the following:
defaults write /full/path/to/newApp.app/Contents/Info.plist LSUIElement 1
Note: You could use LSBackgroundOnly too, but the advantage of LSUIElement is that you can still display UI elements if you want to, such as for debugging.
Important: Substitute the full path of your new app for /full/path/to/newApp.app; the command will only work if you specify the full path to the Info.plist file.
To test, start the new app interactively, and make sure that no Dock icon appears. (You can quit the app via Activity Monitor).
CAVEAT: If the intent is to suppress the system startup sound, set volume with output muted has two drawbacks:
it will not work if headphones happened to be plugged at the time of shutdown
you will have to unmute the volume on startup (however, you could do that in an on on run handler in the same app).
Consider the alternative approach below, which requires admin privileges to set up and invokes nvram SystemAudioVolume=%80 with root privileges, which bypasses the above drawbacks.
You could run do shell script "nvram SystemAudioVolume=%80" user name "someAdminUsername" password "matchingAdminPassword" with administrator privileges from the above AppleScript app, but you'd have to hard-code the password, which is not advisable for security reasons.
Alternative approach, using a system-wide logout hook via com.apple.loginwindow.
There's a deprecated mechanism for running a script on logout that, however, still works as of OSX 10.10; given that there's no direct non-deprecated equivalent, it may continue to be supported.
Note that you do need admin privileges:
sudo defaults write com.apple.loginwindow LogoutHook <yourScript>
<yourScript> must be an executable, such as a shell script; note that the executable is run in the context of the root user.
In case you're thinking of muting the startup sound, invoke the following shell command from that script:
nvram SystemAudioVolume=%80 # to try this interactively, prepend `sudo `
This will mute sounds until after a reboot, effectively muting the startup sound, without keeping the sound muted.
Note that the nvram command requires root privileges, which are by definition in effect in a script run via the com.apple.loginwindow logout hook.; by contrast, to try the command interactively, use sudo nvram SystemAudioVolume=%80 - otherwise, you'll get the following, unhelpful error message: nvram: Error setting variable - 'SystemAudioVolume': (iokit/common) general error
Honestly, it is better to make a deterministic solution. What I mean is, is that you make a script that:
Mutes your computer.
Shuts it down.
Then you take your script and create an Automator service, that you can assign to some shortcut, to make it easier for you to use it. ctrl-opt-cmd-eject or something. :)
This is just how I would have solved it, if I have the need, it is short and sweet to make work, and should work reasonably well.
If you want to use the LogoutHook mentioned in #mklement0's answer.
You can use the normal Applescript command set volume with output muted.
You just need to add the osascript shebang to the top of the Applescript document
i.e
#!/usr/bin/osascript
set volume with output muted
And then save the file as applescript text file.
In the save dialogue use : file format: Text )
It will get the extension .applescript
Once it is saved, use Terminal.app to chmod the script as you would a normal shell script which in effect it is.
i.e
/bin/chmod +x foo.applescript
Then add it to the loginwindows LogoutHook.
sudo defaults write com.apple.loginwindow LogoutHook foo.applescript
I Know this is an old post but for anyone still looking how to do this(like I was) I have a simple method.
Before I started Scripting I created a new folder in my home folder called toolbar scripts.(this is optional)
With the desktop showing Finder click on Go >Utilities >Script Editor.
In the window that opens type in or copy and paste the code
set volume with output muted
tell application "finder"
shut down
end tell
Click on the last button above the script you added - it should be compile. If you cannot find that button then on the top click on Script >Compile
Click on File >Save in the save as I called mine shutdown and chose the script folder (this is optional)
Down the bottom of the dialog box at file format click on the arrow and change the format to application and click on save.
Open the folder you saved it in and drag the icon to the dock. Click on the icon you just put in the dock.
now if all is right this should mute the volume and shutdown the computer.
This will not shutdown the computer if you still have anything open.
Cheers
Peter
first, you should create a sound-off script (with terminal)
sudo nano /Library/Scripts/sound-off.sh
after filling it with these lines:
#!/bin/bash
osascript -e ‘set volume output muted 1’
and make a sound-on script like that
sudo nano /Library/Scripts/sound-on.sh
and fill it with:
#!/bin/bash
osascript -e ‘set volume 4’
then access them as executing files
sudo chmod u+x /Library/Scripts/sound-off.sh
sudo chmod u+x /Library/Scripts/sound-on.sh
and the last part is set them when the mac device is turn off and on:
sudo defaults write com.apple.loginwindow LogoutHook /Library/Scripts/sound-off.sh
sudo defaults write com.apple.loginwindow LoginHook /Library/Scripts/sound-on.sh

Apple script to run a shell script to get around permissions

I have created a shell .command on Mac OS X that I would like to distribute. Based on my testing, you can't execute the .command file by double clicking without changing permissions first ( making executable. ) I don't want users to use the terminal and change permissions, it's too hard for them.
The shell script creates a folder structure for a project, based on where the script is. It needs to be able to run anywhere the user puts it.
The research I have done indicates that I need to use an Apple script to run the file to prompt for the user's password to get around permissions.
Any advice on how to do this?
Distribute the file either by zip or DMG - props Thilo.

Using a bash script on a Mac to connect to network shares?

I'm using a Mac at work and would like some network shares mounting when i start the computer. Can i use a bash script (or similar) to connect and mount these shares? The shares are from both Windows and Mac servers and we usually connect using IP addresses.
You could check out mount_smbfs, assuming that your network shares are smbfs/cifs.
mount_smbfs [-N] [-o options] [-d mode] [-f mode] [-h] //[domain;][user[:password]#]server[/share] path
I'm not intimately familiar with the OSX startup process, but it's possible you can create your shell-script as a program/folder in /Library/StartupItems or /System/Library/StartupItems
As mentioned on osxbook.com
/etc/rc finally launches
/sbin/SystemStarter to handle startup
items from locations such as
/System/Library/StartupItems and
/Library/StartupItems. A StartupItem
is a program, usually a shell script,
whose name matches the folder name.
The folder contains a property list
file containing key-value pairs such
as Description, Provides, Requires,
OrderPreference, start/stop messages
etc. You can run SystemStarter -n -D
as root to have the program print
debugging and dependency information
(without actually running anything).
You can either do this via a Apple Script which you simple add to the User Startup Items, or do it via Automator and again add to Startup Items (System Prefs -> Users -> Startup Items), If you want to you can also just call a bash script from an AppleScript if you are more familier with Bash, but mounting shares is an easy task anyway.
If you maybe like to mount them with a Keystroke, check out FastScript which lets you configure AppleScript Actions as Shortcuts.

Resources