Last active
January 25, 2024 17:13
-
-
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.
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
(* | |
* 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To install
As button in Finder
As service
The workflow is not available under services in finder or you can associate it with a key shortcut as follow: