Skip to content

Instantly share code, notes, and snippets.

@zouyangdev
Last active May 18, 2024 05:36
Show Gist options
  • Save zouyangdev/f3a667334785d6e04fc4e5f7fc4a72fa to your computer and use it in GitHub Desktop.
Save zouyangdev/f3a667334785d6e04fc4e5f7fc4a72fa to your computer and use it in GitHub Desktop.
Create a new empty file in Finder called untitled.txt. If this file name already exists, name it as untitled 2.txt, untitled 3.txt, and so on.
tell application "Finder"
set currentFolder to (the target of the front window) as alias
set baseName to "untitled"
set extension to ".txt"
set fileName to baseName & extension
set maxNumber to 100
set counter to 1
repeat
if not (exists file fileName of currentFolder) then
set newFile to make new file at currentFolder as alias with properties {name: fileName}
select newFile
exit repeat
else
set counter to counter + 1
if counter > maxNumber then
display alert "Maximum naming limit reached" buttons {"OK"}
exit repeat
end if
set fileName to baseName & " " & counter & extension
end if
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment