I can’t work this out and I really need somebody’s help. I have a script which processes some information and then writes an output file to the desktop. All that works well. The problem is that the file always has the same name and thus overwrites an older file with the same name.
I need something like this pseudo code:
– end processing, ready to write the output file MyFile.jpg – this part is done and works.
– here comes where I need help:
if ~/desktop/MyFile.jpg exists than
set addNo to 1
repeat
if (("MyFile " & addNo .jpg) exists = false) then exit repeat
set addNo to addNo + 1
end repeat
end if
set the new file name “MyFile x.jpg” to the first iteration which doesn’t already exists on the desktop
– I will then use that to write the output to the desktop.
Please, can somebody help with a proper code snippet to do that.
set thePath to "/Users/<user>/Desktop/test.csv"
tell (reverse of characters of thePath as text)
tell text 1 thru ((continue offset of "/" in it) - 1) of it
set pop to continue offset of "." in it
if pop = 0 then
set fileExtension to ""
set fileName to reverse of characters of it as text
else
set fileExtension to reverse of characters of (text 1 thru (continue offset of "." in it) of it) as text
set fileName to reverse of characters of (text ((continue offset of "." in it) + 1) thru -1 of it) as text
end if
end tell
set dirName to reverse of characters of text (continue offset of "/" in it) thru -1 of it as text
end tell
set existingFileNames to every paragraph of (do shell script "ls " & quoted form of dirName)
if fileName & fileExtension is not in existingFileNames then
set validName to fileName & fileExtension
else
set x to 1
repeat until fileName & space & x & fileExtension is not in existingFileNames
set x to x + 1
end repeat
set validName to fileName & space & x & fileExtension
end if
set validPath to dirName & validName
There are many, many different ways. To check for existence os a file can be done with do shell script, finder and system events. Splitting the paths can be done with text item delimiters, shell commands or many other ways.
tell application "Finder"
repeat
set mvar to 1
try
set mf to alias ("Myfile" & mvar & ".csv") of folder "Desktop" of home
set mvar to mvar + 1
on error
set mf to (path to desktop as text) & ("Myfile" & mvar & ".csv")
exit repeat
end try
end repeat
end tell
log mf --> myHD:Users:me:Desktop:Myfile1.csv
DJ, many thanks. I inserted your code in my script and it works perfectly. No more file overwrites!
I only changed the path to the actual folder where the output is to be saved - I like clean desktops.
Just one question. You mentioned many more ways of skinning this cat. Could you point me to some reading?
McUsr, many thanks to you as well. I just learned two ways of doing this. Great help guys!
I’ve just returned to my desk and tried to test your script. I think I’m missing something because I can’t make it work. I put two files on my desktop, Myfile.csv and Myfile1.csv. I run your script and expected mf to become Myfile2.csv, but it didn’t, mvar was still 1 instead of 2. What am I missing?
I’d like your script because it’s more compact. Can you help?
It seems shorter but it is longer because it doesn’t work like it should :), at least it has less checks in it.
First block of my code splits the path up (if not needed you can remove the code), second block of code also look ups the original path then when that’s not possible then start (look at the small repeat loop) iterating till a file name is found that doesn’t exists.
My script shortened:
set existingFileNames to every paragraph of (do shell script "ls " & quoted form of POSIX path of (path to desktop))
if "myFile.csv" is not in existingFileNames then
set validName to "myFile.csv"
else
set x to 1
repeat until "myFile" & space & x & ".csv" is not in existingFileNames
set x to x + 1
end repeat
set validName to "myFile" & space & x & ".csv"
end if
set validPath to (path to desktop as string) & validName
tell application "Finder"
set mvar to 1
repeat
try
set mf to item ("Myfile" & mvar & ".csv") of folder "Desktop" of home
set mvar to mvar + 1
on error
set mf to (path to desktop as text) & ("Myfile" & mvar & ".csv")
exit repeat
end try
end repeat
end tell
-- set mf to mf as text
log mf --> myHD:Users:me:Desktop:Myfile1.csv
DJ, I’m really grateful for your help. Your short script works equally well. I decided to use it in my current project because the original path and file name will never change. I also have another more complicated script which will benefit from the additional checks of your longer version, including the split path. The technic there is also very useful for another project I have in mind. i think I need to do some more reading about text handling technics.