-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.applescript
103 lines (88 loc) · 3.08 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- main.applescript
-- Regex Finder Items
-- Created by Annard Brouwer on 27/10/2010.
-- Copyright (c) 2010 Let Software Dream Ltd., All Rights Reserved.
property NameAttributeIdx : 0
property CommentAttributeIdx : 1
property dupFNErr : -48
property paramErr : -50
property userCanceledErr : -128
on run {input, parameters}
local findText, shouldReplace, replaceText, useRegExp, simulate, aPath
set output to {}
set findText to (|findText| of parameters)
set replaceText to (|replaceText| of parameters)
-- Note that some strange hackery is necessary because the values returned at runtime
-- by the bindings are different between Automator and the different runners (Services etc.)!
set useRegExp to (|useRegExp| of parameters)
if (class of useRegExp) is integer then set useRegExp to (useRegExp = 1)
set shouldReplace to (|shouldReplace| of parameters)
if (class of shouldReplace) is integer then set shouldReplace to (shouldReplace = 1)
set simulate to (simulation of parameters)
if (class of simulate) is integer then set simulate to (simulate = 1)
-- check regexp beforehand
if useRegExp then
local strVal
set strVal to call method "validateRegularExpression:" of class "RFIRegexMediator" with parameters {findText}
if (strVal ≠ "OK") then
error strVal number paramErr
end if
end if
-- process input
try
repeat with aPath in input
local oldName, newName
-- aPath is an alias (see Info.plist)
tell application id "com.apple.Finder"
set oldName to the name of aPath
end tell
if shouldReplace then
set newName to call method "find:replace:inText:useRegEx:" of class "RFIRegexMediator" with parameters {findText, replaceText, oldName, useRegExp}
if newName ≠ missing value then
if not simulate then
my renameFileTo(aPath, newName)
set output to output & aPath
else
set output to output & newName
end if
end if
else
local isMatch
if useRegExp then
set isMatch to call method "regularExpression:isMatchedByString:" of class "RFIRegexMediator" with parameters {findText, oldName}
else
considering case
set isMatch to (oldName = findText)
end considering
end if
if isMatch then set output to output & aPath
end if
end repeat
on error errMsg number errNo
if the errNo is not userCanceledErr then log "ERROR: (Regex Finder Items) " & errMsg & " [" & errNo & "]"
error errMsg number errNo
end try
return output
end run
on pathExists(thePath)
try
set thePath to thePath as alias
on error
return false
end try
return true
end pathExists
on renameFileTo(aPath, newName)
local newPath, testPath
tell application id "com.apple.Finder"
set newPath to (parent of aPath) as string
set testPath to (newPath & newName)
if my pathExists(testPath) then
error "Can't rename " & (name of aPath) & ": the renamed item exists already" number dupFNErr
end if
set the name of aPath to newName
end tell
end renameFileTo
on localized_string(aString)
return localized string aString in bundle with identifier "biz.lsd-ltd.otto.RegexFinderItems"
end localized_string