Copy a file from a network folder using applescript - applescript

Iam very new to applescript and am currently trying to copy a file from a network share. I have an iMac server on my network and this script will be distributed to all the mac computers connected to it.
Below is the code i have at the moment :-
set mycopypath to "Volumes:Work Basket:LabRat:LabRat For Client Side.xlsx" as alias
--set mycopypath to choose file
tell application "Finder" to set startup_Disk to (name of startup disk)
set mypastepath to startup_Disk & ":Users:Arjun:Desktop:" as alias
tell application "Finder" to duplicate mycopypath to mypastepath with replacing
In the above code , 'Work Basket' in the 'mycopypath' is the shared network folder. Currently when i run this code i get an error stating - File Volumes:Work Basket:LabRat:LabRat For Client Side.xlsx wasn’t found.
But when i use the commented out part set mycopypath to choose file it works fine. Also, if i use the above code to copy paste a file from one location to another on the local computer itself, it works fine. The network folder "Work Basket" is mounted in both cases.
I'v been at it for quite a few hours and must have tried many combinations of file paths but haven't been able to find a solution.Any help will be great.

Try the following solution:
When you only uncomment the set mycopypath to choose file and comment the rest, you will get the correct path as a result in the bottom half of Script Editor. (See red arrow in screenshot).
After that copy that path to the set mycopypath to "" part and restore the original comments. This should work.
EDIT
I believe this is how to mount a network disk:
tell application "Finder" to mount volume "Your disk path/name"
EDIT 2
Mounting a network disk with user and pass:
mount volume "afp://192.168.200.1/" as user name "your username" with password "your password"

Unlike POSIX paths where a path starts always with a / representing the startup volume and a path to an external volume starts with /Volumes, HFS paths (colon separated) start always with a disk name.
This is a shorter version of your code, the Finder has a property desktop
set mycopypath to "Work Basket:LabRat:LabRat For Client Side.xlsx" as alias
tell application "Finder" to duplicate mycopypath to desktop with replacing

Related

Path to me returning incorrect value on unzipped AppleScript application

So here's a peculiar situation. I created an AppleScript application that relies on the path to me command. After saving as AppleScript Application it runs perfectly. However, if you archive/zip the file, then unzip and run again after downloading/emailing or on another computer, the path to me is incorrect.
For example, I created a super simple app to test this:
set myPath to (path to me as text)
display dialog "myPath: " & myPath
Save the application and run it from the Finder by double clicking it.
Mine reports correctly: myPath: Sierra:Users:joshbooth:Desktop:Test.app:
Zip/Unzip the file on my other laptop and run again.
The result is myPath: 429EF755-2646-4249-A428-666D454E9DAD:d:Test.app:
Option + click and drag to create a new copy in the same directory then run again:
result: myPath: Macintosh HD:Users:joshbooth:Downloads:Test 2.app:
Any ideas what is happening or how to prevent it?
I've no real "answer", but my info is too big for comments (and no one else has chimed in)...
While there may be other cases, at least one cause is a downloaded zip being marked "quarantined" which results in OS X expanding it into a temp "/private/var/folders" location (with the complex folder name you see, as well as the expanded item being quarantined and actually on a read only file system).
So, it is the "download" biting us, which we can see in the "com.apple.quarantine" attribute of the app, and zip, in a terminal window with "xattr -l /Users/johndoe/Downloads/Test.app" (like "com.apple.quarantine: 0083;5f20ce0e;Safari;BFABF751-2ACD-4B6F-9952-9A87D45AC9D4"), and remove it with "xattr -d com.apple.quarantine /Users/johndoe/Downloads/test.app".
Getting (POSIX path of (path to me as string)) within the expanded app would have shown something like /private/var/folders/lh/9hl01d1n2knckzj0bxdb22lr0000gn/T/AppTranslocation/429EF755-2646-4249-A428-666D454E9DAD/d/Test.app before removing the quarantine flag.
Unfortunately, given the temp path is locked, the app itself can't remove the flag with "xattr -d com.apple.quarantine /private/var/folders...", getting "xattr: [Errno 30] Read-only file system", so idea what we can do with this info, but these are my findings.
One good note: moving or copying the expanded app to another folder leaves the quarantine flag set, but updates the "path to me" resolution, which is a bit inconsistent, and confusing, by OS X in my opinion (debatably defeating this quarantine flag pattern if it was intentional), but a useful workaround.

Applescript to rename a file in the applications folder

I'm trying to use a script to rename a text file in the applications folder. I'm trying the following
tell application "Finder"
set name of file "File.txt" of applications folder to "File.txt.OFF"
end tell
But this gives an error:
Can’t get file "Text.txt" of applications folder.
Its definitely in there and called that (it's a copy and paste). I then tried removing the folder bit:
set name of file "File.txt" of applications to "File.txt.OFF"
But got another error:
Finder got an error: Can’t set every application to "Text.txt.OFF".
Any help would be much appreciated.
Cheers
applications folder in Finder terminology does not point to the standard folder /Applications. It seems to be a legacy reference to some pre OS X item.
Try this, but you might not have permission to change the name and you are discouraged anyway from putting arbitrary data like text files into /Applications
set applicationsFolder to path to applications folder
tell application "Finder"
set name of file "File.txt" of applicationsFolder to "File.txt.OFF"
end tell
or use System Events
tell application "System Events"
set name of file "File.txt" of applications folder of local domain to "File.txt.OFF"
end tell

Moving .plist from usb drive to local disk using applescript

I've been trying to figure this one out for a while now, and I've hit a bit of a wall.
I have quite a few Macs in my environment, and for inventory purposes I need to copy a file named "Department-Info.plist" from a USB Drive to the following location on the hard drive
Macintosh HD:Library:Application Support:Dell:KACE:
I've come up with a couple of solutions that I thought might work, and believe I'm on the right track. However, I'm not getting the result I'm looking for.
Below is what I've come up with thus far, but I need to complete this task:
tell application "Finder"
activate
set FileToMove to "VOLUMES:DELL KACE:Mac:Department-Info.plist"
set FinalLoc to "Macintosh HD:Library:Application Support:Dell:KACE:"
copy file FileToMove to FinalLoc
end tell
I receive this error message when running this script:
I've also tried using this variation of the script:
tell application "Finder"
activate
set name of document file "Department-Info.plist" of folder "Mac" of disk "DELL KACE" to "Department-Info.plist"
set FinalLoc to "Macintosh HD:Library:Application Support:Dell:KACE:"
copy "Department-Info.plist" to FinalLoc
end tell
However, this yields the following result.
I wondered if anyone could point me in the right direction, or provide resources that I may have missed.
First of all, the command copy assigns a value to a variable, it does not copy files
The correct code is
tell application "Finder"
-- activate is not needed
set FileToMove to "DELL KACE:Mac:Department-Info.plist"
set FinalLoc to "Macintosh HD:Library:Application Support:Dell:KACE:"
duplicate file FileToMove to folder FinalLoc
end tell
However consider that you might not have sufficient access privileges to copy the file.
In this case you need to use the shell to enter an admin password:
do shell script "cp '/Volumes/DELL KACE/Mac/Department-Info.plist' '/Library/Application Support/Dell/KACE/'" with administrator privileges

Applescript Error: Finder got an error: Can’t get folder "~/Library/Services"

Here is my code:
tell application "Finder"
move POSIX file "/Volumes/Toggle Desktop Icons/Toggle Desktop Icons/Install Files/Hide Icons.workflow" to folder "~/Library/Services"
end tell
And it always gets this error: Can’t get folder "~/Library/Services".
Any help? This also needs to work on any mac I run the code on. Without changing the code
Thanks.
There is a system property path to home folder that you could use. And for simplicity sake use the native mac HFS path delimiter ":" to reference the rest of your path.
Try this (adding back in your full source path),
tell application "Finder"
move POSIX file "/Volumes/.../Hide Icons.workflow" to folder (((path to home folder) as text) & "Library:Services")
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