I think the difficulty is that you are flying blind:
- Two distinct evaluation spaces (Applescript can’t see what the shell is up to)
- A mutating loop which leaves no trace.
You can lose less time in debugging by making things visible:
- Try to do everything in one evaluation space (all in AppleScript, for example)
- Use a map, which can show you all the values generated, rather than an immediately mutating loop, which is a blind process.
If I wanted an update to some property of a set of records (as in the title adjustments of your performSmartRule example), I would probably generate two visible sets of values (existing titles, and then updated title values), to check that they are what I wanted, before committing them to my record titles:
e.g.
Existing titles of some selected records -->
{"What I Wish I Knew When Learning Haskell 2.2 ( Stephen Diehl )",
"Translation of “Monads are just Monoids in the Category of Endofunctors”
| tailcalled", "tackling the Awkward squad.pdf",
"Recommended Haskell reading and watching - link", "mpickering - Automatically Apply HLint Suggestions",
"Lecture notes and assignments.webloc", "lambdaCalculus.pdf",
"Imperative Functional Programming.pdf"}
What their values would be if transformed by some function (e.g. toUpper
) -->
{"WHAT I WISH I KNEW WHEN LEARNING HASKELL 2.2 ( STEPHEN DIEHL )",
"TRANSLATION OF “MONADS ARE JUST MONOIDS IN THE CATEGORY OF ENDOFUNCTORS”
| TAILCALLED", "TACKLING THE AWKWARD SQUAD.PDF",
"RECOMMENDED HASKELL READING AND WATCHING - LINK", "MPICKERING - AUTOMATICALLY APPLY HLINT SUGGESTIONS",
"LECTURE NOTES AND ASSIGNMENTS.WEBLOC", "LAMBDACALCULUS.PDF",
"IMPERATIVE FUNCTIONAL PROGRAMMING.PDF"}
And then finally, if things look OK, uncomment and recomment to get a version which updates the records:
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
on run
tell application id "DNtp"
set xs to selection
script smartRuleApplied
on |λ|(x)
-- 1. Check existing values
name of x
-- 2. Check values returned by some function (here just case-change)
-- toUpper(name of x)
-- 3. Update values
-- set name of x to toUpper(name of x)
end |λ|
end script
my map(smartRuleApplied, xs)
end tell
end run
-- GENERAL FUNCTIONS
-- https://github.com/RobTrew/prelude-applescript
-- A SIMPLE TEST FUNCTION TO APPLY
-- toUpper :: String -> String
on toUpper(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toUpper
-- MAP RATHER THAN LOOP, TO KEEP THINGS VISIBLE
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn