-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtagdeletionbycount1
More file actions
82 lines (62 loc) · 2.97 KB
/
Copy pathtagdeletionbycount1
File metadata and controls
82 lines (62 loc) · 2.97 KB
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
-- EFFICIENT DELETION SCRIPT for Single-Item Tags
-- This version uses batch processing to delete all targeted tags in a single command,
-- which is significantly faster.
-- Define lists to hold the results.
set tagsToDelete to {}
set tagNamesToDelete to {}
try
-- === PASS 1: FINDING (No changes here) ===
-- This block finds all the tags that meet our criteria.
tell application id "DNtp"
set theDatabase to current database
if not (exists theDatabase) then error "No database is open."
set theTagsGroup to get record at "/Tags" in theDatabase
set allTagRecords to children of theTagsGroup
set totalTagCount to count of allTagRecords
if totalTagCount is 0 then
error "No tags found in the database to check."
end if
show progress indicator "Finding tags with 1 item..." steps totalTagCount
repeat with aTagRecord in allTagRecords
set theTagName to name of aTagRecord
step progress indicator "Checking: " & theTagName
set foundRecords to lookup records with tags {theTagName} in theDatabase
if (count of foundRecords) is 1 then
set end of tagsToDelete to aTagRecord
set end of tagNamesToDelete to theTagName
end if
end repeat
hide progress indicator
end tell
set deletionCount to count of tagsToDelete
if deletionCount is 0 then
display dialog "Process complete. No tags were found with exactly 1 item."
return
end if
-- === CONFIRMATION DIALOG (No changes here) ===
set AppleScript's text item delimiters to ", "
set tagListForDisplay to tagNamesToDelete as text
set AppleScript's text item delimiters to ""
display dialog "Found " & deletionCount & " tags with exactly 1 item each." & return & return & "Are you sure you want to permanently delete these " & deletionCount & " tags in a single batch?" & return & return & "Example Tags: " & tagListForDisplay buttons {"Cancel", "Delete All"} default button "Cancel" cancel button "Cancel"
-- === PASS 2: EFFICIENT DELETING (This is the new part) ===
-- We send a single 'delete' command with the entire list of tags.
tell application id "DNtp"
-- The progress indicator is "indeterminate" because we can't step through a single command.
show progress indicator "Deleting " & deletionCount & " tags in a single batch..." steps -1
-- *** THE KEY EFFICIENCY IMPROVEMENT IS HERE ***
-- Delete the entire list of records at once.
delete record tagsToDelete
hide progress indicator
end tell
-- === FINAL REPORT ===
set AppleScript's text item delimiters to return
set finalReportText to tagNamesToDelete as text
set AppleScript's text item delimiters to ""
display dialog "Deletion Complete!" & return & return & "Successfully deleted " & deletionCount & " tags:" & return & return & finalReportText
on error errMsg number errNum
-- Make sure the progress indicator is hidden even if an error occurs.
try
tell application id "DNtp" to hide progress indicator
end try
display alert "An error occurred:" & return & "Error " & errNum & ": " & errMsg
end try