I'm trying to replicate the function of the volume keys, but in applescript. I can't get it to work though. The increase volume button puts the volume at max and the decrease volume button does the same. Anyone know what i'm doing wrong? Heres my code:
-- increase volume
on increaseVolumeHandler_(sender)
tell application "finder"
set theOutput to output volume of (get volume settings)
set volume output volume (theOutput + 6.25)
end tell
do shell script "afplay /System/Library/Sounds/Pop.aiff"
end increaseVolumeHandler_
-- decrease volume
on decreaseVolumeHandler_(sender)
tell application "finder"
set theOutput to output volume of (get volume settings)
set volume output volume (theOutput - 6.25)
end tell
do shell script "afplay /System/Library/Sounds/Pop.aiff"
end decreaseVolumeHandler_
Those functions work for me on 10.7.5, what OSX version are you trying on?
Also you could remove the redundant code of the sender argument and the tell finder block, e.g,
on increaseVolumeHandler_()
set theOutput to output volume of (get volume settings)
set volume output volume (theOutput + 6.25)
do shell script "afplay /System/Library/Sounds/Pop.aiff"
end increaseVolumeHandler_
Related
I need some help with an apple script that I can save on the computer so others can remove the ._ files from USB drives that are used for slide shows on TVs. I come from Linux so I pseudoed what I could.
usb_drives=$(ls /volumes/ | grep external) # intent
MyFlashDrive=$(prompt "Please select drive" usb_drives) # intent
dot_clean -m /Volumes/${MyFlashDrive} # mostly accurate
The AppleScript version of this would be something like the following:
tell application "System Events"
set ejactableDisks to get (displayed name of every disk whose ejectable is true)
--name of every file of home folder whose name begins with "."
end tell
set targetDiskName to choose from list ejactableDisks
tell application "System Events"
set targetDisk to first disk whose name is targetDiskName
set dotUnderscoreFiles to every file of targetDisk whose name begins with "._"
if (count of dotUnderscoreFiles) is greater than 0 then
delete dotUnderscoreFiles
end if
end tell
Of course, if you're more comfortable with unix you could write a bash routine and run it through do shell script, or just make an executable bash script.
I'm trying to use AppleScript to set the label of a file to a specific colour; or rather, I already have a script that used to work, but ever since upgrading to High Sierra it no longer does.
I've stripped it down to the absolute basics (always sets label to green):
on run theArguments
set theFile to POSIX file (item 1 of theArguments)
tell application "Finder" to set (theFile's label index) to 6
end run
If you save this to a file (green.scpt) then you can run it from Terminal with the following command:
osascript ~/Downloads/green.scpt ~/Downloads/green.scpt
(substitute the paths for wherever you store the script)
This should turn the script's label in the Finder to green, but doesn't (at least on High Sierra), instead giving the following error message:
/Users/haravikk/Downloads/green.scpt: execution error: Finder got an error: Can’t set label index of file "Users:haravikk:Downloads:green.scpt" to 6. (-10006)
Am I doing something wrong here? If not, and this is a bug, then is there some other way to change a file's label via script?
You can try to use tell application before change label index
on run theArguments
tell application "Finder"
set thisItem to POSIX file theArguments as alias
if label index of thisItem is not 6 then
set the label index of thisItem to 6
end if
end tell
end run
This works for me (added "as alias"):
on run theArguments
set theFile to POSIX file (item 1 of theArguments) as alias
tell application "Finder" to set (theFile's label index) to 6
end run
Note though that labels are long gone, and have been replaced with tags.
As part of a larger project I would like to get the path of the Mount Point of the device which hosts my script.
I have designed the following code in Script Editor, however, upon implementing it in ApplescriptObjC (Xcode), I noticed that error started occuring.
# Get Mount Point of script USB host
tell application "Finder"
try
set usbMountPoint to text 1 thru -2 of POSIX path of (disk of (path to me) as alias)
log usbMountPoint # Output: /Volumes/usb-drive-name
on error
set usbMountPoint to POSIX path of (disk of (path to me) as alias) # if file is on desktop, path: '/', can't have '[space]' hence no removing
log usbMountPoint # Output: /Volumes/usb-drive-name
end try
end tell
Can’t make «class ocid» id «data optr00000000207F220080600000» into type constant. (error -1700)
I have included comments in the code as to how the logged output should look.
I understand that similar questions regarding the path to me problem in Xcode have been asked before, and that set myPath to current application's NSBundle's mainBundle()'s bundlePath() gives the current path, however, my question is concerning the disk of part of it.
Any help in getting the desired output in Xcode is greatly appreciated.
It requires a lot of coercions but the AppleScriptObjC equivalent in Xcode is
set myPath to current application's NSBundle's mainBundle()'s bundlePath() as text
tell application "Finder" to set mountPoint to POSIX path of (disk of item (myPath as POSIX file as text) as alias)
log mountPoint
The startup volume is represented by / all other volumes by /Volumes/<volumeName>
On Mac OS 10.10, I'm trying to create an alias of a mounted volume and put the alias in the user's home folder so this can later be put into the dock. The volume is an SMB share called satffdata$ (it's a hidden Windows share, hence the $ at the end). I've tried different variants of the paths, such as explicitly defining it and using the built in alias's but to no avail.
Please see code below and then output. The error code I'm getting and what I've read suggest that the problem could be it's seeing the paths as strings, however I can't explicitly define the startup disk name or the user profile as these may be different at the time the script is automatically run by our Casper Suite system.
tell application "Finder"
activate
try
display dialog "Setting your StaffData$ (P Drive) Shortcut. Press OK to continue."
mount volume "smb://servername/staffdata$"
set staffData to POSIX path of "/Volumes/staffdata$"
set homeDir to POSIX path of (path to home folder)
display dialog staffData
display dialog homeDir
make new alias to staffData at homeDir
set name of result to "staffdata$"
end try
end tell
Output is as follows:
tell application "Finder"
activate
display dialog "Setting your StaffData$ (P Drive) Shortcut. Press OK to continue."
--> {button returned:"OK"}
mount volume "smb://servername/staffdata$"
--> error number -10004
end tell
tell application "Script Editor"
mount volume "smb://servername/staffdata$"
--> file "staffdata$:"
end tell
tell application "Finder"
display dialog "/Volumes/staffdata$"
--> {button returned:"OK"}
display dialog "/"
--> {button returned:"OK"}
make new alias to "/Volumes/staffdata$" at "/"
--> error number -10000
end tell
If you've got any questions, I'll respond as soon as I can. Thanks!
The main problem of the script is, the Finder does not accept POSIX paths.
The dictionary of the Finder has a property home representing the home folder and disk objects can easily specified by name
Try this
display dialog "Setting your StaffData$ (P Drive) Shortcut. Press OK to continue."
try
set volumeName to "staffdata$"
mount volume "smb://servername/" & volumeName
tell application "Finder"
if not (exists alias file volumeName of home) then
make new alias file to disk volumeName at home
set name of result to volumeName
end if
end tell
on error e
display dialog e
end try
I am trying to write an AppleScript that will check if a network drive (in this case, my Time Capsule) is mounted and, if not, mount it. I've figured out how to mount the Time Capsule, but I am at a loss over how to have the script check whether it is mounted first and just exit if it is, or mount it if not.
tell application "Finder"
mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
end tell
Does that the job?
set mountedDiskName to "AirPort Time Capsule"
set diskIsMounted to false
tell application "System Events" to set diskNames to name of every disk
if mountedDiskName is in diskNames then
set diskIsMounted to true
end if
if diskIsMounted then
log "Disk Found, unmounting now..."
do shell script "diskutil unmountDisk" & space & quoted form of mountedDiskName
else
log "Disk Not Found, mounting now…"
mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
end if
Update
I thought you want to unmount the disk when mounted but that was wrong :) here a shorter version:
tell application "System Events" to set diskNames to name of every disk
if "AirPort Time Capsule" is in diskNames then
display dialog "Disk already mounted" buttons {"OK"} default button 1 with icon 1
else
mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
end if
…or, if you want the disk name in the dialog:
set diskName to "AirPort Time Capsule"
tell application "System Events" to set diskNames to name of every disk
if diskName is in diskNames then
display dialog quoted form of diskName & " is already mounted." & return buttons {"OK"} default button 1
else
mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
end if
You don’t have to check through a list of all disks. You can just ask if the disk you want exists.
tell application "Finder"
if not (disk "Airport Time Capsule" exists) then
mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
end if
end tell
It seems it doesn't matter whether a share is already mounted or not when using the mount command on Finder with apple script. Just mount it anyway, Finder handles this correctly (it doesn't mount it a second time nor does it complain loudly).