AppleScript, check if folder exist in current users home directory - applescript

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

Related

Applescript: Need Help Resolving - Can't Get every file of said folder?

With the help of some users on this form I was able to create this script below which lets you choose a folder then choose which folders within the folder you want to rsync into a OneDrive Backup Folder.
Basically were migrating from a on site network storage to OneDrive for Business and want to create a script that as easy as possible for our users.
The issue I have is I dont want the users to be able to choose the original folder, I want to set the variable up front.
In the script I use:
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?
set OuserName to do shell script "whoami"
set userName to do shell script "echo " & OuserName & " | tr a-z A-Z"
tell application "Finder"
if not (disk userName exists) then
mount volume "SMB Server/" & userName & "$"
end if
delay 2
set theDialogText to "
- Mac H-Drive Migration Tool -
This Application Will Migrate a Copy of Your H Drive Data
to your OneDrive for Buisness Folder Locally on Your Mac
Migration Backup Location:
/Users/" & OuserName & "/OneDrive Folder/H-Drive Migration Backup
** Important **
In the Next Window Please Choose
The Root Folder of Your H Drive
The Drive Label Should Look Like: " & userName & "$"
display dialog theDialogText
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
do shell script "mkdir -p ~/'OneDrive Folder'/'H-Drive Migration Backup'"
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
set AppName to "OneDrive.app"
tell application "Finder" to set Answer_ to exists application file ((path to applications folder as string) & AppName)
if Answer_ is false then
beep
beep
beep
beep
beep
end if
delay 1.5
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
end tell
repeat with thisName in theChosenNames
tell application "Terminal"
do script ("rsync -avpz --delete " & (quoted form of POSIX path of ((theFolder as text) & thisName)) & space & (quoted form of POSIX path of HDriveBackupFolder)
end tell
end repeat
end tell
You asked:
"How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?"
Also, just prior to that, you said:
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
First, lets address setting the value of the theFolder variable. It should be as follows:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
I assume the error is being thrown at the following point in the code:
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
Change set theFolderNames to name of folders of theFolder to:
set theFolderNames to name of folders of container theFolder
To test this, I mounted a volume named MYERSMI5$ at /Volumes/ and created some folders within /Volumes/MYERSMI5$/. Then running the following code:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
tell application "Finder"
activate
set theFolderNames to name of folders of container theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
end tell
It produced a list box, containing the names of the folders I created at that location, to choose from.
I did not try to run the entire block of code you included in your question, so if you're having other issues, you'll need to follow up after making the changes mentioned in my answer. There are good reasons why questions involving debugging code should conform to How to create a Minimal, Complete,and Verifiable example.
You may also want to review the Variables and Properties section in the AppleScript Language Guide.

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 Posix chaos

Yet again flummoxed by syntax and ordering of an applescript workflow solution
When I create the following code
tell application "Finder"
set remote_p to alias (POSIX file '/Volumes/WAM XSAN/Audio/AAA)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
Everything works fine. However, I need to create the main_folder in different locations dependent on the input "client_code" and "department", So i tried this:
tell application "Finder"
set x_san to "/Volumes/WAM XSAN/"
set x_sannewpath to (x_san & department & "/" & client_code)
set x_sanfolder to POSIX file x_sannewpath
set remote_p to alias (POSIX file x_sanfolder)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
And the error comes back "Can't get "Volumes/WAM XSAN/Audio/AAA" of Application Finder"
Where am i going wrong in setting up the POSIX paths?
Please help!!!!
Try this example. I tried to model it after what you're trying to do. Note that I have moved most of the code outside the Finder tell block of code. The Finder does not know the "posix file" command. That's an applescript command, not a Finder command. Also you don't need the Finder to set variables and add strings together. We can do all of that outside the Finder.
Even though it sometimes works when you have the "posix file" command inside a Finder tell block of code, it's always best if you only tell applications to do what they know how to do. You can find what an application knows how to do by looking in its applescript dictionary. Check the Finder dictionary and you will see that "posix file" is not one of its commands.
Anyway, this will create a folder on your desktop. I hope this helps.
set x_san to "/Users/hmcshane/"
set client_code to "Desktop"
set temp_name to "test folder"
set x_sannewpath to x_san & client_code
set applescript_x_sannewpath to POSIX file x_sannewpath
tell application "Finder"
set main_folder to (make new folder at applescript_x_sannewpath with properties {name:temp_name}) as alias
end tell
Try:
set department to "Audio"
set client_code to "AAA"
set temp_name to "New Folder"
set x_san to "Volumes/WAM XSAN/"
set x_sannewpath to x_san & department & "/" & client_code
tell application "Finder" to set main_folder to (make new folder at POSIX file x_sannewpath with properties {name:temp_name})

does System Events in AppleScript support duplicating files?

I've been trying to get System Events to duplicate files in AppleScript and I've been failing :) I eventually always get the error "error "Files can not be copied." number -1717". So I changed my tactics and tried using the Finder to make sure what i was trying to do was correct. Here is the code that works:
tell application "System Events"
set desktopFolder to (path to desktop folder) as string
set fullPath to desktopFolder & "Temp Export From DO"
set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
set source to path of DOEntry
log "Source file: " & source
set destination to fullPath as string
log "Destination folder: " & destination
tell application "Finder"
duplicate file source to folder destination with replacing
end tell
end repeat
end tell
If I remove that last tell, so that it uses System Events, I get the same error noted above. The dictionary for System Events standard suite has a "duplicate" command so I'm not sure what is going on here. Also, "Learning AppleScript, 3rd ed" from APress notes:
"One particularly annoying omission in System Events is that it can’t yet duplicate files and folders; if you need to do this, the Finder is your best bet."
The 3rd edition is from 2010. It would seem that even in Mountain Lion this is still true. Can anyone confirm this? The 1717 error number lists everywhere else as a handler error and i'm not using handlers.
Unfortunately, you cannot duplicate files using System Events - you have to use the Finder. Even in the answer provided by adayzdone, System Events is not actually handling the duplication.
This looks like it's working (because it's inside a System Events tell block)...
tell application "System Events"
duplicate myFile to myFolder
end tell
...but if you inspect the event log you'll see that the Finder is actually performing the duplication. Behind the scenes, you are passing two Finder objects to System Events. System Events doesn't know how to handle Finder objects, so execution is passed to the objects' owner, the Finder, which executes the command.
For file duplication in AppleScript, you are unfortunately limited to using the Finder or the command line via do shell script.
Try:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
log "Source file: " & DOEntry
log "Destination folder: " & desktopFolder
tell application "Finder" to duplicate file DOEntry to desktopFolder with replacing
end repeat
If you don't need to log the values you can simply:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
tell application "Finder" to duplicate theDOEntries to desktopFolder with replacing
Or:
set desktopFolder to quoted form of (POSIX path of (path to desktop folder as text) & "Temp Export From DO")
do shell script "find '/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries' -name \"*.doentry\" -type f -print0 | xargs -0 -I {} cp -a {} " & desktopFolder
Getting back to your question, the duplicate commands creates duplicates of Finder items. You can use System Events to duplicate Finder items like this:
tell application "Finder"
set myFile to file ((path to desktop as text) & "Test File.txt")
set myFolder to folder ((path to desktop as text) & "Test Folder")
end tell
tell application "System Events"
duplicate myFile to myFolder
end tell

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