forked from jmcantrell/applescript-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRename Substitution.scpt
69 lines (55 loc) · 2.02 KB
/
Rename Substitution.scpt
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
66
67
68
69
on getRequiredInput(promptText, defaultValue)
set promptPrefix to ""
set promptIcon to note
repeat
tell application "Finder" to display dialog promptPrefix & promptText default answer defaultValue with icon promptIcon
set replyValue to text returned of result
try
if replyValue = "" then error
exit repeat
on error
set promptPrefix to "INVALID ENTRY! "
set promptIcon to stop
end try
end repeat
return replyValue
end
on getInput(promptText, defaultValue)
tell application "Finder" to display dialog promptText default answer defaultValue with icon note
return text returned of result
end
on pathsToFiles(thePaths)
set theFiles to {}
repeat with thePath in thePaths
set end of theFiles to POSIX file thePath
end repeat
return theFiles
end
on quotePaths(theFiles)
set thePaths to ""
repeat with theFile in theFiles
set thePaths to thePaths & " " & quoted form of (POSIX path of (theFile as alias))
end repeat
return thePaths
end
on renameFiles(theFiles)
if count of theFiles equals 0 then return
tell application "Finder"
display dialog "Are you sure you want to rename these " & (count of theFiles) & " files?" with icon caution
set homeFolder to POSIX path of (path to home folder) as string
set renameCommand to homeFolder & ".local/bin/rename-sub"
set pattern to my getRequiredInput("Enter pattern:", "")
set replacement to my getInput("Enter replacement:", "")
set renameCommand to renameCommand & " -P " & quoted form of pattern
set renameCommand to renameCommand & " -R " & quoted form of replacement
set renameCommand to renameCommand & " " & my quotePathes(theFiles)
reveal my pathsToFiles(paragraphs of (do shell script renameCommand))
end tell
end
on open (theFiles)
renameFiles(theFiles)
end
on run
tell application "Finder" to set theFiles to selection
renameFiles(theFiles)
end