forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-new-file.applescript
executable file
·65 lines (55 loc) · 1.48 KB
/
create-new-file.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/osascript
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Create New File
# @raycast.mode silent
# Optional parameters:
# @raycast.icon 📄
# @Documentation:
# @raycast.description Create files in the front window or desktop of the visit
# @raycast.author LokHsu
# @raycast.authorURL https://github.com/lokhsu
set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false
-- get folder path
try
tell application "Finder"
set this_folder to folder of the front window as alias
end tell
on error -- no open folder windows
set this_folder to path to desktop folder as alias
set is_desktop to true
end try
-- get the new file name (do not override an already existing file)
tell application "Finder"
set file_list to name of every file of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
if new_file is in file_list then
set new_file to file_name & x & file_ext
set x to x + 1
else
exit repeat
end if
end repeat
-- create and select the new file
tell application "Finder"
activate
set the_file to make new file at folder this_folder with properties {name:new_file}
if is_desktop is false then
reveal the_file
else
select window of desktop
set selection to the_file
delay 0.1
end if
end tell
-- press enter (rename)
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell