-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* | |
| * 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