Make new folder on server with applescript - applescript

I am trying to create a new folder on a server with a name that was taken from the user. Here is what I have so far
display dialog "Please enter your augnet username" default answer "username"
set username to (text returned of result)
mount volume "smb://orgs.augsburg.edu"
tell application "Finder"
make new folder at "orgs.augsburg.edu/Information Technology/www/kb_images/Migration Testing" with properties {name:username}
end tell
I am getting an error saying "cannot make into type item"
any help is much appreciated. Thanks!

You have an error because :
1 - The type of this path is "posix path", the Finder does not recognize this type, slashes should be replaced by colons
2- You must specify the class (folder, files, disk, ...) when you use a string (the path) in the Finder
display dialog "Please enter your augnet username" default answer "username"
set username to (text returned of result)
mount volume "smb://orgs.augsburg.edu"
tell application "Finder"
make new folder at folder "orgs.augsburg.edu:Information Technology:www:kb_images:Migration Testing:" with properties {name:username}
end tell

Related

AppleScript choose file dialog box store last folder opened in text file.

This seems like such an easy thing to do that I'm amazed the solution is still eluding me.
I'm trying to store that last folder opened by a "choose file" dialog box. I want to store that folder's location in a text file.
I want to have my "choose file" dialog always open to the last folder used.
I've got a lot of the script working but there is one weird thing that keeps eluding me.
Look at my script...
set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")
set strPathFromConfig to item 1 of Shows as string
set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)
display dialog strPathFromConfig
set strPath to (path to home folder as text) & strPathFromConfig
display dialog strPath
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath
The script reads my config text file which contains one line and only one string of "Documents".
I trim some leading garbage characters and display a dialog with the result of "Documents".
Then I set the strPath using the the value from the config file.
I display the new value is it is a valid location on my system.
Next I attempt to the dialog and I get an error message of "File alias Macintosh HD:Users:lowken:Documents of wasn't found.
Let change the script so that instead of using the value that was extracted from the config.txt file, I simply set a string variable in my script.
set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")
set strPathFromConfig to item 1 of Shows as string
set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)
display dialog strPathFromConfig
set strTemp to "Documents"
set strPath to (path to home folder as text) & strTemp
display dialog strPath
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath
Now it works. AppleScript seems to not want to use the value that was looked up from the config.txt file.
What am I doing wrong? I tried casting to an Alias I tried different locations on my system.
It seems to me that somehow the value looked up from the text file is not a string data type.
Any ideas?
P.S.
I'm running OS X Yosemite 10.10.4 on a mid 2012 MacBook Pro.
The problem is the encoding of your "config.txt" document.
The read command can read (MacRoman, UTF-8 or UTF-16) encoded text files, but you must specify the type class, otherwise it read the text file as MacRoman.
set strPathFromConfig to paragraph 1 of (read "/Users/lowken/Documents/config.txt" as «class ut16») -- or if UTF-8 use —> as «class utf8»
set strPath to ((path to home folder as text) & strPathFromConfig) as alias
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location strPath
For another encoding, you must change the encoding of the "config.txt" document.
The default location of the choose file command can be a Unix path "/Users/my_user/Documents" or a Finder path with Alias before like : alias "HD:Users:my_User:Documents"
so first check that strPath is the correct value, then if OK, double check the class of it :
Display Dialog (class of strPath) as string
it may not be OK and you have to coerce the (path to home folder as string) and not text.

Applescript Choose file, filename

What I'm trying to make is something like this:
set filename to (choose file)
display dialog "The name of the chosen file is: " & filename
But when I use that script it will return a path to the file something like .../.../.../.../nameoffile.jar. But what I want is that it will return the filename and not the path (so just only nameoffile.jar). Is there a way to do that?
Thanks for reading, please give an answer as soon as possible ;),
Jort
Use the Finder Suite:
set chosenfile to (choose file)
tell application "Finder"
display dialog "The name of the chosen file is: " & (name of chosenfile)
end tell

applescript help naming .plist and removing extention

i am creating an applescript to create a file with property list elements but without the .plist extension!
my issue is if i use a dialog to get the name of the file eg.
tell application "SystemUIServer"
display dialog "Enter filename :- " buttons {"Generate file"} default answer "Generate Keyfile"
set fileName to text returned of result
and create the file on the desktop like so
set text_file to (path to desktop)'s POSIX path & "" & quoted form of fileName & ".NEWextention"
finally i add elements to the .plist
tell application "System Events"
tell (make new property list file with properties {name:text_file})
make new property list item at end with properties {kind:string, name:"regName", value:"FOO"}
make new property list item at end with properties {kind:string, name:"regNumber", value:"BAR" as text}
end tell
end tell
end tell
however the file that is created is has '' when quoted from fileName and still has the .plist extention.
eg input :- myfile
output:- 'myfile'.NEWextention.plist
when i want
myfile.NEWextention
how can i achieve this in applescript?
any help would be greatly appreciated!
many thanks in advance.
below is the fixed code thanks to #McUsr
without his help i would have been at the same point for over 6 months of trial and error
tell application "SystemUIServer"
display dialog "Enter FileName :- " buttons {"Generate file"} default answer "Generate file"
set FileName to text returned of result
set text_file to (path to desktop folder as text) & FileName & ".plist"
set dateStamp to do shell script "date"
tell application "System Events"
tell (make new property list file with properties {name:text_file})
make new property list item at end with properties {kind:string, name:"Name", value:FileName}
make new property list item at end with properties {kind:string, name:"Date", value:dateStamp}
end tell
end tell
end tell
tell application "Finder"
set name extension of file text_file to "newExt"
end tell
You can make it happen afterwards you are done processing it with System Events, by Finder, (set name extension of file "Hfs:path:to:file:with:name.ext" to "new-ext").
But I am not sure if you can expect System Events to regard the file as a property list file, containing property list items afterwards. It is still worth a try though. :)
This is how you must change the name extension, as this don't work with System Events (name extension is a read only property).
tell application "Finder"
set mf to (path to desktop folder as text) & "labels.txt"
set name extension of file mf to "text"
end tell
Use the HFS path of the file, aka: Macintosh Hd:Users:You:path:file.ext, and not the posix path. And don't use quoted form of det path.

Error when trying to duplicate files to shared server

I'm trying to get duplicate home folders (desktop, movies, music, home, etc..) to a shared server. I was finally able to get the drive mounted using a concatenation of string and variables. But now i'm getting an error on the duplication line. If I mount the drive manually, this code works, but doesn't seem to work when mounting the drive through applescript
--Assigns variable to username
tell application "System Events"
set user_name to name of current user
end tell
--Prompts for eraider as string
set returnedName to (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1 giving up after 5)
--Sets string as variable
set work_name to text returned of returnedName
--Sets department list
set deptList to {"dept1", "dept2", "dept3", "dept4"}
set dept_name to {choose from list deptList} as string
--Tells Finder to mount shared drive
tell application "Finder"
mount volume ("smb://vserver/athletics/"&dept_name&"/"&work_name)
end tell
--Sets sever as variable
set vserver to POSIX file ("/Volumes/" & work_name)
--Copies Documents to Server
set source to POSIX file ("/Users/" & user_name & "/Documents")
tell application "Finder"
duplicate source to vserver with replacing
end tell
I get this error
error "Finder got an error: AppleEvent handler failed." number -10000
On this line: duplicate source to vserver with replacing
You have a few strange things in your code. Most of them do not affect your error however they should be corrected. For example the "mount volume" command is not a Finder command, it's an applescript command and thus you should not tell the Finder to perform that command. Next, the "choose file" line... why do you have brackets around it? Plus other small things. So I cleaned up the code.
To get to your specific problem it could be 2 things. First, I use an if statement to ensure the disk is mounted before trying to duplicate the files. Second, the "POSIX File" command could also cause problems. It's an unusual command in that it doesn't always work the way you expect. Therefore in this code, to ensure we avoid any issues with that command, I coerce it to text and then in the duplicate command I put it in the proper format by using the word "folder" in front of it.
So I did not test this code but it should work. I hope it helps.
--Assigns variable to username
tell application "System Events" to set user_name to name of current user
--Prompts for eraider as string
set work_name to text returned of (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1 giving up after 5)
--Sets department list
set deptList to {"dept1", "dept2", "dept3", "dept4"}
set dept_name to item 1 of (choose from list deptList)
--mount shared drive
mount volume ("smb://vserver/athletics/" & dept_name & "/" & work_name)
if work_name is in (list disks) then
--Copies Documents to Server
set source to (POSIX file ("/Users/" & user_name & "/Documents")) as text
tell application "Finder" to duplicate folder source to disk work_name with replacing
else
display dialog "Disk " & work_name & " is not mounted"
end if
An even better way would be to use the "path to" command to establish the path to your documents folder. This way you don't need to use "POSIX File" or even know the user name because the "path to" command automatically works for the current user. Plus I would do a little more error checking on the mount volume stuff. So really this is how I would write your code... I didn't test this either!
--Prompts for eraider as string
set work_name to text returned of (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1 giving up after 5)
--Sets department list
set deptList to {"dept1", "dept2", "dept3", "dept4"}
set dept_name to item 1 of (choose from list deptList)
--mount shared drive
set isMounted to false
if work_name is not in (list disks) then
mount volume ("smb://vserver/athletics/" & dept_name & "/" & work_name)
-- wait for the volume to be mounted but only wait for a limited time before failing (10 seconds in this case)
set inTime to current date
repeat
delay 0.2
if work_name is in (list disks) then
set isMounted to true
exit repeat
end if
if (current date) - inTime is greater than 10 then exit repeat
end repeat
else
set isMounted to true
end if
--Copies Documents to Server
if isMounted then
set source to path to documents folder
tell application "Finder" to duplicate source to disk work_name with replacing
else
display dialog "There was an error mounting disk " & work_name buttons {"OK"} default button 1
end if

Applescript copy files to mounted volume; test for file(s) already exists

I am writing a script to allow students to upload their files to a shared folder on teacher's computer in a computer lab (same network). I have a working script that, when executed, will duplicate all files in the folder UPLOAD on the student machine to the folder SUBMISSIONS on the teacher's machine. However, if a file already exists on the teacher machine, the script hangs.
I need to be able to test for the presence of individual files on the teacher machine and either (a) pop a message that says "this file already exists, rename it and upload again" or (b) append something to the file name to differentiate it...a random number or "copy 1" etc.
Ideally I want it to run as a folder action. When a file is added to the "UPLOAD" folder it will automatically be sent to the teacher. But I don't want files to copy over files of the same name...or for the script to hang.
Any thoughts or alterative approaches would be welcome.
Here's my code:
set home_path to path to home folder as string
set work_folder to alias (home_path & "Desktop:" & "Upload")
try
mount volume "afp://[LOGIN INFO].local/Submissions"
set this_folder to result as alias
tell application "Finder"
tell application "Finder"
duplicate every file of work_folder to this_folder
end tell
eject this_folder
end tell
end try
I think that it would help if your try-block had an on-error block to inform you about any errors.
try
# try stuff here
j # this will compile but will throw a runtime error and you can see the error
on error error_message number error_number
display alert "Error: " & error_message & return & return & (error_number as text)
end try
OK. I tried again to write some code. Here is my version.
It does a couple of things differently than the code posted by the original poster.
It copies the files and folders into a new folder on the server with a time-stamp to cope with the problems of whether some of the files already exist on the server.
I changed the wording of the duplicate statement from duplicating every "file" to duplicating every "item" so that folders are duplicated too.
I put in an on-error block in the try-statement to display any errors.
I activate Finder so that you can see the progress window.
I pop up a dialog at the end if there were no errors.
I had a problem that I had to fix:
On the server, I had to enable write permissions for the client or I got a -5000 error.
I think that the following code should work pretty well. It's almost 100% AppleScript. It only uses one call to the shell and that is to get the current date and format it for the time-stamp for new folders created on the server.
# set the path to the "work_folder" where the files are to be uploaded
set home_path to path to home folder as string
set work_folder to alias (home_path & "Desktop:" & "Upload")
# duplicate the files and folders in work_folder to the server
try
# TODO set the name of your server here
set the_volume to mount volume "afp://32-bit.local/Submissions"
set destination_path to the_volume as text
set folder_name to getTimeStamp()
tell application "Finder"
activate
set new_folder to make new folder at alias destination_path with properties {name:folder_name}
duplicate every item of work_folder to new_folder
eject the_volume
display alert "Successfully uploaded the files and folders"
end tell
on error error_message number error_number
if error_number is not equal to -128 then
display alert "Error: " & error_message & return & return & (error_number as text)
end if
end try
# This function returns the current date and time as a time-stamp of the form yyyy-mm-dd-hh-ss
# This function uses a shell script because it's just a lot easier to do than in AppleScript
on getTimeStamp()
set time_stamp to do shell script "date '+%Y-%m-%d-%H-%M-%S'"
return time_stamp
end getTimeStamp
Here's another idea for debugging. You can put in calls to "display dialog" to be able to know where your script is failing:
display dialog "Entering script"
set home_path to path to home folder as string
display dialog "home_path: " & home_path
set work_folder to alias (home_path & "Desktop:" & "Upload")
display dialog "work_folder: " & work_folder as string
try
mount volume "afp://32-bit.local/Submissions"
set this_folder to result as alias
display dialog "this_folder: " & this_folder as string
tell application "Finder"
display dialog "Inside of the first tell application Finder"
tell application "Finder"
display dialog "About to call duplicate"
duplicate every file of work_folder to this_folder
display dialog "Just returned from calling duplicate"
end tell
display dialog "About to call eject"
eject this_folder
display dialog "Just returned from call to eject"
end tell
on error error_message number error_number
display alert "Error:" & error_message & return & return & (error_number as text)
end try
display dialog "Exiting script"
Another debugging technique is to log output to a text file.
Another debugging technique is to purchase the AppleScript debugger:
http://www.latenightsw.com/sd4/index.html
I believe that this debugger costs $200.00 that's too pricey for me, but I've used other debuggers and debuggers are wonderful tools that let you "peek inside" of your script while it's running to see the value of variables and to trace which lines of code are being executed.
This script will copy each file over to the mounted volume. If a file exists with the same name at the destination it will add a number to the end of the copy file name and try that.
Example:if test.doc exists in the folder already then the script will try and copy it with the name test_1.doc and so on..
The original file is never renamed and the older files are never overwritten.
The script is fully commented to explain what it is doing.
** Update2 **
The copy to destination code is now in it's own handler.
The original files are labeled by the finder label index 6 (green) to indicate successful copied.
This will also stop the same original file from being copied twice by using the index colour as a check. If it is labeled index label 7 it will be ignored.
You could if you want move the successful copied files to another folder out of the way using the script. But I have not done this in this script.
set home_path to path to home folder as string
set work_folder to alias (home_path & "Desktop:" & "Upload")
set counter to ""
global counter
--try
set this_folder to mount volume "afp://myMac/UserName/"
this_folder as text
tell application "Finder" to set theFiles to every file of work_folder as alias list #GET ALL FILES OF LOCAL FOLDER AS ALIASES
tell application "Finder" to set theRemoteFiles to (every file of ((this_folder & "Submissions:" as string) as alias)) #GET ALL FILES OF REMOTE FOLDER -- ONLY NEEDED FOR COUNT CHECK LATER
repeat with i from 1 to number of items in theFiles #PROCESS EACH LOCAL FILE
set this_item to item i of theFiles #GET A LOCAL FILE
tell application "Finder" to set LabelIndex to label index of this_item
if LabelIndex is not 6 then
tell application "Finder" to set this_name to displayed name of this_item #GET ITS DISPLAYED NAME
tell application "Finder" to set this_extension to name extension of this_item #GET ITS EXTENSION NAME i.E "txt"
set realName to (do shell script "echo " & quoted form of this_name & "| awk -F '" & quoted form of this_extension & "' '{print $1}'") #GET ITS NAME WITHOUT EXTENSION NAME
set counter to 1 # SET A NUMBER TO ADD TO THE FILE NAME IF THE FILE NAME EXIST ALREADY IN THE REMOTE FOLDER
my checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder) # CALL TO HANDLER THAT WILL DO THE CHECKING AND COPYING
end if
end repeat
tell application "Finder" to eject this_folder
# THE CALL TO THE HANDLER INCLUDES VARIABLES THAT ARE NOT GLOBAL OR PROPERTIES BUT NEED TO BE PASSED ON TO IT I.E(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder)
on checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder)
# (1) IF THE NUMBER OF theRemoteFiles IS GREATER THAN 0 THEN FILES EXIST IN THE REMOTE FOLDER AND MAY CONTAIN FILES WITH THE SAME NAMES AS THE LOCAL ONES. PROCESS..
# (2) IF THE NUMBER OF theRemoteFiles IS NOT GREATER THAN 0.THEN FILES DO NOT EXIST IN THE REMOTE FOLDER AND THE LOCAL ONES CAN JUST BE COPIED OVER.
if (count of theRemoteFiles) is greater than 0 then # (1)
try
my copyOver(this_item, this_folder, this_name)
on error errMssg #WE USE not overwritten ERROR TO TRIGGER THE RENAME THE DESTINATION FILE NAME TO INCLUDE A NEW NUMBER.
--tell application "Finder" to set label index of this_item to 6
if errMssg contains "not overwritten" then
set this_name to (realName & counter & "." & this_extension)
set counter to counter + 1 #WE SETUP THE FILE NAME NUMBER FOR THE POSSIBLE NEXT RUN
# RUN THE HANDLER AGAIN WITH THE CHANED DETAILS
my checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder)
end if
end try
else # (2)
my copyOver(this_item, this_folder, this_name)
end if
end checkName
on copyOver(this_item, this_folder, this_name)
# THE -n OPTION IN THE SHELL COMMAND TELLS CP NOT TO OVERWRITE EXISTING FILES. AN ERROR OCCURE IF THE FILE EXISTS.
# THE -p OPTION IN THE SHELL COMMAND TELLS CP TO PRESERVE THE FOLLOWING ATTRIBUTES OF EACH SOURCE FILE IN THE COPY:
# modification time, access time, file flags, file mode,
#user ID, and group ID, as allowed by permissions. Access Control
#Lists (ACLs) and Extended Attributes (EAs), including resource
#forks, will also be preserved.
# THE -v OPTION IN THE SHELL COMMAND TELLS CP TO USE VERBOS MODE. WHICH GIVES US A BETTER CLUE OF THE ERROR
set theResult to (do shell script "cp -npv " & quoted form of (POSIX path of this_item) & space & quoted form of (POSIX path of (this_folder & "Submissions:" as string) & this_name))
if theResult contains "->" then
tell application "Finder" to set label index of this_item to 6
else
tell application "Finder" to set label index of this_item to 7
end if
end copyOver

Resources