Hi, I have written a basic script (all my scripts are still basic at this stage) for creating a text file on the desktop. This file is needed for another script to reference, so I wanted to automate it’s creation on different computers. The script works fine, but there is a code block that is repeated, so I wanted to make it a function. As soon as I do this it fails, with an error saying Finder can’t continue with the function.
Here is the script:
global desktopPath
set desktopPath to path to desktop
global fileName
set fileName to "Computer.txt"
global filePath
set filePath to (desktopPath & fileName) as string
on makeFile()
display dialog "Which computer is this?" buttons {"Main", "Backup"}
set buttonResult to button returned of the result
make new file at desktopPath with properties {name:fileName, type:"text"}
set newFile to open for access filePath with write permission
write buttonResult to newFile
close access newFile
end makeFile
tell application "Finder"
-- check if computer file exists
if file filePath exists then
display dialog "Computer file already exists, delete and recreate?" buttons {"Yes", "No"} default button 1 cancel button 2
delete file filePath
makeFile()
else
makeFile()
end if
end tell
The script checks to see if the file exists, if not, creates the file, if it does it asks if it should recreate the file. Creating the file asks a question, the answer to which is written as the contents of the text file.
If I replace the two points where the function is called with the code block from within the function then it works perfectly. But I get Finder cannot continue with makeFile() when it’s in a function. What am I not doing right here?
thank you
ps. I wasn’t sure how to put in code, so let me know if there is a better way.