Skip to content

Instantly share code, notes, and snippets.

@shredthaGNAR
Forked from avaccari/createFile.applescript
Created September 23, 2022 22:40
Show Gist options
  • Select an option

  • Save shredthaGNAR/ce4695c98f6d491c28137f4f57184a92 to your computer and use it in GitHub Desktop.

Select an option

Save shredthaGNAR/ce4695c98f6d491c28137f4f57184a92 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.
(*
* An script to create a new empty file based on the current selection in Finder:
* - if there is nothing selected, it will create the file in the currently display folder
* - if a file is selected, it will create the file in the folder of the selected file
* - if a folder is selected, it will create the file in the selected folder
* - if an application is selected, it will not create the file
*
* By default the name extention of the file is .txt. If the function key
* is pressed while calling this script, the user will be prompted for the
* extension.
*)
(* from https://gist.github.com/Grayson/1154126 *)
on isOptionKeyPressed()
return (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSFunctionKeyMask > 1'") is "True"
end isOptionKeyPressed
tell application "Finder"
set currentSelection to selection
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
if my isOptionKeyPressed() then
set extension to display dialog "File extension?" default answer ".txt"
set fileExt to text returned of extension
end if
set newFile to fileName & fileExt
make new file at thisFolder with properties {name:newFile}
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment