Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript Help

Hey guys,


I have a script that I've written, but I have a small issue with it - wondering if any one can lend a hand.

Some background - I have large excel files - this script will take my selected column values, and create folders out of the names to a specified folder.

My issue is that if there are duplicate values in the Excel sheet, the script will get an error. I know that usually you can add 'with replacing' but I believe since mine says 'with properties' that overrides 'with replacing'

Hope this makes sense. Script attached:

set rowAList to getCellList("G", 2, 41)

tell application "Microsoft Excel" to set allDocs to name of every document
set myDoc to choose from list allDocs


set myFolder to (choose folder)

repeat with i in rowAList
  tell application "Microsoft Excel" to tell document (myDoc as string) to tell sheet 1 to set cellValue to (value of cell i)

  tell application "Finder" to make new folder at myFolder with properties {name:cellValue} with replacing

end repeat


on getCellList(aLetter, startNumber, endNumber)
  set myList to {}
  repeat with i from startNumber to endNumber
  set end of myList to (aLetter & i) as string
  end repeat
  return myList
end getCellList

iMac, OS X Yosemite (10.10.2)

Posted on Mar 23, 2015 2:16 PM

Reply
Question marked as Best reply

Posted on Mar 23, 2015 3:11 PM

Well, the most obvious solution involves three words: try... end try.


Put the line(s) of code that is throwing the error (presumably the 'tell application "Finder" block) inside a try/end try block. If any errors occur they will be caught within the try block and not blow out the rest of the script.


try

tell application "Finder" to makenewfolderatmyFolderwith properties {name:cellValue} with replacing

end try


A second approach is a proactive check - see if the folder already exists before issuing the 'make new folder' command.


Either approach is valid. Which is best really depends on what you want to do if the folder exists. If you want to silently fail then the try approach may be easier. If you want additional logic (e.g. rename/move the existing folder, empty it, etc.) then a pre-emptive check may be better

.

2 replies
Question marked as Best reply

Mar 23, 2015 3:11 PM in response to BVanrooy

Well, the most obvious solution involves three words: try... end try.


Put the line(s) of code that is throwing the error (presumably the 'tell application "Finder" block) inside a try/end try block. If any errors occur they will be caught within the try block and not blow out the rest of the script.


try

tell application "Finder" to makenewfolderatmyFolderwith properties {name:cellValue} with replacing

end try


A second approach is a proactive check - see if the folder already exists before issuing the 'make new folder' command.


Either approach is valid. Which is best really depends on what you want to do if the folder exists. If you want to silently fail then the try approach may be easier. If you want additional logic (e.g. rename/move the existing folder, empty it, etc.) then a pre-emptive check may be better

.

Mar 23, 2015 3:17 PM in response to Camelot

That makes perfect sense, I know that it is in the

try

tell application "Finder" to makenewfolderatmyFolderwith properties {name:cellValue} with replacing

end try

section of code. I also know that the folders its making are completely empty, so in the end all that I need to do is overwrite the folders if the script ever finds duplicates. Which is why the 'with replacing' command would be perfect, if I could make that work. They are always creating empty folders from an excel sheet - What I've been doing is just removing duplicates from Excel, but a properly coded script would alleviate the time spent doing that.

Thanks for the help

Applescript Help

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.