I have a spreadsheet with two columns – name and qty.
I intend to modify qty by passing name as parameter to the applescript.
I have achieved the desired result. Its working correctly. However, I would like to know if it is the right way to do so.
The handler below creates a new list and then sets it to the sheet. Is this how records (“records” as used in spreadsheet context, not DEVONthink Pro dictionary) are supposed to be modified?
on modifyList(theList_, newQty_, theSearchTerm_)
-- if name does not match, add aItem to theList2 else
-- add newQty to (item 2 of aItem) and add this new item to theList2
set theList2 to {}
repeat with aItem_ in theList_
set a to item 1 of aItem_
if a is not theSearchTerm_ then
set end of theList2 to aItem_
else
set oldQty to item 2 of aItem_ as number
set newQty_ to oldQty + newQty_
set theCells to {theSearchTerm_, newQty_, ""}
set end of theList2 to theCells
end if
end repeat
return theList2
end modifyList
on setChildCells(dbName, recordName, childName, aList)
tell application id "com.devon-technologies.thinkpro2"
tell its database dbName
tell its record recordName
tell its child childName
set cells to aList
end tell
end tell
end tell
end tell
end setChildCells
If it is indeed working correctly, this is your answer.
Approaches to coding are often very personalized. I write the way I write. You write the way you write. This doesn’t mean you can do whatever you want (since you have to work within the confines of the language and app dictionary), but you may use many variables, I may use many handlers. Also, remember that part of the joy of coding is learning more than going back and refactoring your previous work (though it is sometimes a little embarrassing to see some of the old, naive code we wote ).
Thanks for the reply.
Let me clarify. To modify value of just one field in one record, I am creating a new list from scratch and replacing the old one, thus, effectively re-writing the entire spreadsheet contents. This suggests me that I must be doing something wrong. Or is there no other way to do this?
I am passing the parameter to my AppleScript from another app so that DEVONthink Pro works in background and then shows a notification about the updated quantity. So, I am assuming, the above would not work.
Secondly, I would like to know, what is not “tight” in the syntax used in my handler? I just followed basic heirarchial structure from the dictionary.
tell its database dbName
tell its record recordName
tell its child childName
set cells to aList
end tell
end tell
end tell
```or
tell the content record
set cells to aList
end tell
[quote]
I just followed basic heirarchial structure from the dictionary.
[/quote]
You will come to find (hopefully), there is (almost) always ways to make your code more concise. The dictionary is a guide, not the law. :smiley:
Personally, I have always been a fan of the one-liner, or getting the most out of the least amount of code. But my code - even written for corporations - has been written and maintained BY ME. (Anything that needed to be seen by others was written looser and commented thoroughly though.)
When I am in another app, , I have no idea what DEVONthink Pro’s visible document is. I want to get the modification of record (as used in spreadsheet context) done in the background.
How would DEVONthink Pro know which content record are we talking to unless we specify the database name, record name (as used in DEVONthink Pro context) and the child name?
Script Debugger’s Explorer view, which gives you a real time view of the DT object model, is unbelievably helpful in figuring this stuff out. Seeing the object model being laid out in a tree structure and being able to inspect any object’s value often gives a whole new perspective on a problem. When you cant figure out the proper way to address and object, its possible to drag the object straight out of the explorer into your code and get the right code.
Late Night Software has a 20 day free trial which will be enough if you only want to debug one or two scripts quickly. I bought it when it was still the twice the price it is now and have never regretted it. It’s saved me hours.
DT has a hierarchy of windows and items in those windows. Thus you can get from DT the content record of the top most window, or the content record of the selected item (at least in the same space) without having to know which database its in.
I appreciate the help.
I really don’t know why I am not being understood.
I am not in DEVONthink Pro when I am running the script (I already mentioned this at least twice in this thread). I am in other application (Alfred). When the script is being run, DEVONthink Pro may not even be open, leave alone knowing which database or record shall be “current”. How can I even think of referring the relevant child record without going through the object heirarchy (i.e. database, record, child, cells etc?
The “tighter” code would work when I am in DEVONthink Pro and know what is “content record”.
use lib_one : script "devon-think_basic.scpt"
on run arg
set dbName to "Shop"
set recordName to "stock"
set childName to "godown_gi_fitting"
try
-- set theSearchTerm to "tn 40"
-- set newQty to -2
set newQty to my getQty(arg)
set theSearchTerm to my getName(arg)
set theList to lib_one's getChildCells(dbName, recordName, childName)
set theList2 to my modifyList(theList, newQty, theSearchTerm)
lib_one's setChildCells(dbName, recordName, childName, theList2)
tell application "Alfred 3" to run trigger "showStock" in workflow "com.rounak.gi_stock" with argument theSearchTerm
on error e
end try
end run
on getQty(sourceText)
set sourceText to sourceText as text
set AppleScript's text item delimiters to ","
set emptyQty to last text item of sourceText
set emptyQty to emptyQty as number
set AppleScript's text item delimiters to ""
return emptyQty
end getQty
on getName(sourceText)
set sourceText to sourceText as text
set AppleScript's text item delimiters to ","
set emptyString to text items 1 thru -2 of sourceText
set AppleScript's text item delimiters to " "
set emptyString to emptyString as text
set AppleScript's text item delimiters to ""
return emptyString
end getName
on modifyList(theList_, newQty_, theSearchTerm_)
-- if name does not match, add aItem to theList2 else
-- add newQty to (item 2 of aItem) and add this new item to theList2
set theList2 to {}
repeat with aItem_ in theList_
set a to item 1 of aItem_
if a is not theSearchTerm_ then
set end of theList2 to aItem_
else
set oldQty to item 2 of aItem_ as number
set newQty_ to oldQty + newQty_
set theCells to {theSearchTerm_, newQty_, ""}
set end of theList2 to theCells
end if
end repeat
return theList2
end modifyList
devon-think_basic.scpt
on getChildCells(dbName, recordName, childName)
tell application id "com.devon-technologies.thinkpro2"
tell its database dbName
tell its record recordName
tell its child childName
get cells
end tell
end tell
end tell
end tell
end getChildCells
on setChildCells(dbName, recordName, childName, aList)
tell application id "com.devon-technologies.thinkpro2"
tell its database dbName
tell its record recordName
tell its child childName
set cells to aList
end tell
end tell
end tell
end tell
end setChildCells