Create Alias of Mounted Volume in User Home with AppleScript - applescript

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

Related

AppleScript, check if folder exist in current users home directory

I'm trying to create a script that checks if a folder exist on the currents users desktop,
tell application "Finder"
set RAIDFolder to POSIX path of (path to desktop) & "RAID"
if exists folder RAIDFolder then
display notification "" with title "RAID" subtitle "FOUND IT " & RAIDFolder
else
display notification "" with title "RAID" subtitle "NOT FOUND " & RAIDFolder
end if
end tell
But I cannot get it to work, it always goes to display notification:
"NOT FOUND /Users/edit02/Desktop/RAID
Which is the correct path its looking for. I'm fairly fresh in applescripts, and scripting in general, som any help to why I can't get it to display that it detected the folder is appreciated.
This is for a script for several editing stations which all have different usernames, and I want it to check for a folder on desktop, and if missing sync folder from a local raid.
Also, would a symlink folder show as a folder to a if exist foldercommand?
I have tested both.
Thanks John I.
The problem is that you are not thinking like AppleScript — in particular, you are not thinking like the scriptable Finder. It doesn't think in posix paths. If you're going to convert to a posix path, you've basically stepped outside the Finder's world of thought — and then you can't come back to it and expect it to understand the posix path.
If you just want to know whether there is a folder called "RAID" on the desktop using the Finder and AppleScript, simply say
tell application "Finder"
exists folder "RAID" of desktop
end tell
That results in true if there is such a folder and false otherwise.
If you insist on talking about posix paths, then don't talk to the Finder. Just say, totally outside of any app tell block:
set deskpath to get POSIX path of (get path to desktop)
set raidfile to POSIX file (deskpath & "RAID")
Now if you want to know whether that exists as a folder, you can ask the Finder. But when you do, it will throw an error if it doesn't, so you have to catch that error:
tell application "Finder"
try
exists folder raidfile
on error
false
end try
end tell
Here is how I'd code it:
set RAIDFolder to POSIX path of (path to desktop) & "RAID"
tell application "System Events" to set theFolderExists to exists folder RAIDFolder
if theFolderExists then
display notification "" with title "RAID" subtitle "FOUND IT " & RAIDFolder
else
display notification "" with title "RAID" subtitle "NOT FOUND " & RAIDFolder
end if

Applescript to ask for input, verify folder exists, and then open folder

I want to write a script using Automator that opens a folder in a particular location, after receiving user input. Doesn't have to be Applescript.
So the steps would be:
Dialog asking for Folder name
Verifying the folder exists
If exists, open folder in new finder window
if not exist, display message
Any help would be greatly appreciated.
instead of asking user to type folder name into a dialog box, why not use the standard "choose folder" which provide usual file/folder selection graphic interface ? on top of that, it will also make sure the folder selected exists !
Also it is possible to user instruction "if My_Folder exists then ..." to check if folder (or file) exists
Example of direct user selection : 5 first lines are asking folder selection in folder Documents and detect user cancellation. Next lines are just example to display result
try
set SelectFolder to choose folder with prompt "choose folder" default location "/Users/My_User/Documents"
on error number -128
set SelectFolder to ""
end try
if SelectFolder is "" then
display alert "user did not select a folder"
else
display alert "User selection is " & SelectFolder
end if
The following script does exactly what you're asking for.
on run
set thisFolder to (choose folder with prompt "Choose a folder...")
if folderExists(thisFolder) then
-- display dialog "The folder exists." buttons {"OK"} default button 1
tell application "Finder" to open thisFolder
else
display dialog "The folder has been deleted" buttons {"OK"} default button 1
end if
end run
on folderExists(theFolder)
tell application "Finder"
try
set thisFolder to the POSIX path of theFolder
set thisFolder to (POSIX file thisFolder) as text
--set thisFolder to theFolder as alias
return true
on error err
return false
end try
end tell
end folderExists
That said, note that a folder that has been selected using AppleScript's Choose Folder command must always exist. It can't be selected if it doesn't exist. Therefore, my first thought is that you don't need this, but if you need to check whether a folder exist for a different reason, then the folderExists function will do the job.
You could use the following script to do what you're asking for (no choose folder, this script uses a dialog like you asked for):
set folderChosen to text returned of (display dialog "Enter the path to the folder you want to open:" default answer "")
try
do shell script "ls " & folderChosen
do shell script "open " & folderChosen
on error
display alert "Folder Doesn't Exist" message "The folder path you entered doesn't exist. Make sure to enter a path, e.x. /Users/USERNAME/MyFolder." as critical
end try
This AppleScript uses do shell script "ls " & folderChosen to verify if the folder exists, and then opens the folder if ls successfully runs, indicating the folder path exists. Otherwise, it displays an alert with a warning sign explaining the path they entered doesn't exist.
If you want this to run in automator, you could use Automator's Run AppleScript feature.
I hope this solution helps you solve your problem!

Applescript droplet doesn't run another handler

I'm trying to make a droplet script in Applescript using the code below.
I have a handler which runs a command line with the POSIX path of the selected file.
When I run the script everything works (my command line handler works fine). But, if I drop the file to my script, nothing happens.
I need some help, please
on run
set thePath to POSIX path of (choose file of type "img" default location (path to downloads folder from user domain))
doIt(thePath)
end run
on open myImageFile
tell application "Finder"
if (count of myImageFile) is greater than 1 then
display alert "A message" as critical
else if name extension of item 1 of myImageFile is not "img" then
display alert "A message" as critical
else
set thePath to POSIX path of item 1 of myImageFile
doIt(thePath)
end if
end tell
end open
Change the line: doIt(thePath) in the open handler into: my doIt(thePath).
The reason for the error, is that you try to use your handler from within the scope of finder, and finder, doesn't reckognize it as something of its own. Therefore you must add myin front of it, so the handler can be resolved.

Copy mounted folders to local folder

We have a samba share from where I'd like to copy folders with an applescript. This is what I already have (the mounting works):
mount volume "smb://samba.com/e_18_data11$"
delay 3
set sourcefolder to ("smb://samba.com/e_18_data11$/e_18_data11$/folder1/folder2" as POSIX file)
set localfolder to ("/Users/username/Dropbox/Test" as POSIX file)
tell application "Finder" to duplicate sourcefolder to localfolder
This gives me still this error:
the routine can not edit objects of this class." number -10010
I tried and combined many solutions already on SO, e.g. this solution
– OS X 10.9
It's probably the sourcefolder specification that is wrong.
I think you can just use the volume name instead of "smb://".
set sourcefolder to ("/Volumes/7samba.com/e_18_data11$/e_18_data11$/folder1/folder2" as POSIX file)
(if the mounted volume is named "7samba.com")
Tip: drag the actual sourcefolder from Finder into your AppleScript. It should paste the path into the script. Use that path for sourcefolder.
More:
The Error your getting is:
Mac OS error -10010 (telBadHTypeErr): bad hook type specified
I tested it (with two local folders) to see if the script would work. It did work and duplicated the folder.
You can (or should anyway) wrap critical code into a try block, like this:
try
duplicate sourcefolder to localfolder
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try
This way you can check and react to errors.
Addition:
May be you can check for existence like this:
tell application "Finder"
set aBoolean1 to get (exists sourcefolder)
set aBoolean2 to get (exists localfolder)
end tell
log aBoolean1
log aBoolean2
Both bool's must be YES

AppleScript Droplet on DMG not working

Hey I have the following AppleScript saved as a Droplet.
It is saved on a DMG file like this one http://dl.dropbox.com/u/1839051/TestDMG.dmg
The Problem is, while some can drag the template onto the Droplet and have it working, when I try to drag the template onto the droplet the crossed-out circle-symbol shows up indicating that this action is not possible. Nothing happens, the file is not copied.
Does anyone have any idea why I have this problem and how it can be fixed?
Thanks in advance guy.
on open thefiles
set outputFolder to (path to application support folder from user domain as text) & "iWork:Pages:Templates:My Templates:"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of outputFolder
tell application "Finder"
duplicate thefiles to outputFolder
end tell
end open
Rather than using a droplet and having the user to drag the files onto the droplet, why not just make an installer so the user only has to double-click the installer? It would be easier and also probably avoid your problem. I also added some error handling in your code because it's just prudent to do that with shipping code. We also tell the user what happened.
NOTE: you also had an error in your code. The outputFolder is a string. The Finder requires a file specifier. To make the string into a specifier you add either the word "file" or "folder" in front of the string path. Your code may have worked but the proper way to write it is with a specifier. Other applications may not take the string path but they will all take the specifier... so get in the habit of using them instead of strings.
try
-- create the output folder if necessary
set outputFolder to (path to application support folder from user domain as text) & "iWork:Pages:Templates:My Templates:"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of outputFolder
-- find the templates on the dmg disk
set myPath to path to me
tell application "Finder"
set myContainer to container of myPath
set templateFiles to (files of myContainer whose name extension is "template") as alias list
end tell
-- copy the templates to the output folder
-- NOTE: the script will error if any of the templates already exist
-- therefore we use a repeat loop and duplicate each file separately with a try block
-- around it to avoid errors in case some templates have already been installed.
tell application "Finder"
repeat with aTemplate in templateFiles
try
duplicate aTemplate to folder outputFolder
end try
end repeat
end tell
-- tell the user everything was OK
tell me to activate
display dialog "The templates were successfully installed! You may now use them in Pages." buttons {"OK"} default button 1 with title "Templates Installer" with icon note
on error
tell me to activate
display dialog "There was an error installing the templates. Please manually install them by copying them to the following folder." & return & return & (POSIX path of outputFolder) buttons {"OK"} default button 1 with title "Templates Installer"
end try
This looks to be a permissions issue, and I have to wonder if the differential between those who can and those who can't have something to do with which OS they are running. I'm running Mac OS 10.6 as an administrator and I was unable to perform the action in the DMG. But I was able to perform the action if I dragged both files out of the DMG and onto my Desktop.
If you need to install files in specific locations to the hard drive to support your project, then I would recommend making an installer (and a matching uninstaller as well) as opposed to the setup you have presented.

Resources