Skip to content

Instantly share code, notes, and snippets.

@cmittendorf
Last active August 29, 2015 14:04
Show Gist options
  • Save cmittendorf/23164c09325d8940c4ad to your computer and use it in GitHub Desktop.
Save cmittendorf/23164c09325d8940c4ad to your computer and use it in GitHub Desktop.
Create new files or folders with a keystroke and by using the Tool FastScripts (http://www.red-sweater.com/fastscripts/) you can overwrite the default Finder Shift-Command-N shortcut to create a new folder for invoking our new script.
(**
Create new files or folders with a keystroke and by using the Tool
FastScripts http://www.red-sweater.com/fastscripts/
you can overwrite the default Finder Shift-Command-N shortcut
to create a new folder for invoking our new script.
**)
tell application "Finder"
-- default name for a new file following
-- the Finders convention for naming folders
set newFileName to "untitled file"
-- get the current folder
set pwdAlias to insertion location as alias
set pwd to POSIX path of pwdAlias
--
set result to choose from list {"File", "Folder"} with prompt ¬
"Make new File or Folder" default items {"File"} ¬
without multiple selections allowed
if result is not false then
if item 1 of result is "File" then
set newFileName to my freeFileName(pwd & "/" & newFileName, 0)
do shell script "touch '" & newFileName & "'"
select item (newFileName as POSIX file)
else if item 1 of result is "Folder" then
set newFolder to make new folder at pwdAlias
select newFolder
end if
end if
end tell
on freeFileName(name, index)
-- prepare the new name of the new file
set fileName to name
if index > 0 then
-- use the same naming convention as the Finder uses for folders
if index = 1 then
set index to 2
end if
set fileName to name & " " & index
end if
-- check if the file exists
tell application "Finder" to if exists fileName as POSIX file then
-- increment the counter and test again
set newIndex to index + 1
return my freeFileName(name, newIndex)
else
-- if not, return the valid name
return fileName
end if
end freeFileName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment