Skip to content

Instantly share code, notes, and snippets.

@avaccari
Last active January 25, 2024 17:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save avaccari/6c0a117aa323bca21484cc45ccc37923 to your computer and use it in GitHub Desktop.
Save avaccari/6c0a117aa323bca21484cc45ccc37923 to your computer and use it in GitHub Desktop.
Apple script to create a file with desired extension in a folder. Can be used in automator to create an app or a quick action.
(*
* Apple script to create a new file/folder based on the current selection in Finder:
* - if there is nothing selected, it will create the file/folder in the currently displayed folder
* - if a file is selected, it will create the file/folde in the folder of the selected file
* - if a folder is selected, it will create the file/folder in the selected folder
* - if an application is selected, it will not create the file/folder
*
* Notes:
* - By default it creates files named "untitled.txt".
* - Pressing "shift" will prompt the user for the file name
* - Pressing "ctrl+shift" will create a folder named "untitled"
* - Pressing "cmd+shift" will prompt the user for the folder name
*)
(* from https://forum.latenightsw.com/t/detect-option-key-state/3165 *)
use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
use framework "AppKit"
on keyPressed()
set modifier to (current application's NSEvent's modifierFlags())
if modifier is 131072 then -- shift (define new file name)
return "shift"
else if modifier is 393216 then -- ctrl + shift (new "untitled" folder)
return "ctrlShift"
else if modifier is 1179648 then -- cmd + shift (define new folder name)
return "cmdShift"
else if modifier is 0 then
return "none"
end if
end keyPressed
tell application "Finder"
set currentSelection to selection
set folderName to "untitled"
set fileName to "untitled"
set fileExt to ".txt"
if currentSelection is {} then
set thisFolder to the target of the front window
else if kind of item 1 of currentSelection is "Application" then
display dialog "No file will be added to an application"
return
else if kind of item 1 of currentSelection is "Folder" then
set thisFolder to currentSelection
else
set thisFolder to parent of item 1 of currentSelection
end if
set modifierKey to my keyPressed()
if modifierKey is "shift" then
set newFile to display dialog "File name?" default answer "untitled.txt"
set newFile to text returned of newFile
make new file at thisFolder with properties {name:newFile}
else if modifierKey is "ctrlShift" then
make new folder at thisFolder with properties {name:folderName}
else if modifierKey is "cmdShift" then
set folderName to display dialog "Folder name?" default answer "untitled"
set folderName to text returned of folderName
make new folder at thisFolder with properties {name:folderName}
else if modifierKey is "none" then
set newFile to fileName & fileExt
make new file at thisFolder with properties {name:newFile}
end if
end tell
@avaccari
Copy link
Author

To install

As button in Finder

  • Open automator
  • Create a new application
  • Select run applescript as action
  • Paste the script
  • Save the application
  • Drag and drop the application to the Finder toolbar while pressing command

As service

  • Open automator
  • Create a new quick action
  • Select no input in finder
  • Select run applescript as action
  • Paste the script
  • Save the quick action (workflow)
    The workflow is not available under services in finder or you can associate it with a key shortcut as follow:
  • Open system preferences
  • Select keyboard
  • Select services on the left
  • The action should be available at the very bottom of the list on the right under general

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment