Created
November 11, 2010 21:30
-
-
Save josht/673235 to your computer and use it in GitHub Desktop.
planswift random item# generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will recursively check if the new randomid is already in use | |
function Exists(randomid:String;path:string):Boolean | |
Begin | |
// If no path is set, then the search starts in the Storages root | |
if (path = '') then begin | |
path := '\Storages'; | |
end; | |
curItem := Planswift.GetItem(path); | |
if (curItem.GetPropertyResultAsString('Item#') = randomid) then begin | |
Result := true; | |
end; | |
else begin | |
if (curItem.ChildCount > 0) then begin | |
for i:= 0 to curItem.ChildCount - 1 do begin | |
Result := Exists(randomid,path + '\' + curItem.ChildItem(i).Name); | |
if (Result) then | |
Exit; | |
end; | |
end; | |
else begin | |
Result := false; | |
end; | |
end; | |
End; | |
// This will generate our psuedo-random id | |
Function GenRan(category,type:String):String; | |
begin | |
ranNum := FloatToStr(random()); | |
ranNum := copy(ranNum,3,6); | |
itmCat := copy(category,0,Length(category)- length(category) + 3) | |
itmType := copy(type,0,Length(type)- length(type) + 3) | |
Result := itmCat + '-' + ranNum + '-' + itmType; | |
End; | |
begin | |
BeginUpdate; | |
ths := ItemPath; | |
// Retrieve the GUID of the currently selected item | |
AItem := Planswift.Selecteditem.GUID; | |
// If an Item# is already set, we don't want to change it | |
if GetResultAsString(AItem,'Item#','') <> '' then begin | |
Exit; | |
end; | |
itemCat := GetResultAsString(AItem,'Category',''); | |
if (itemCat = '') then begin | |
ShowMessage('You must select a category before generating an Item#'); | |
Exit; | |
end; | |
itemType := GetResultAsString(AItem,'Type',''); | |
nRan := GenRan(itemCat,itemType); | |
i := 0; | |
exst := exists(nRan,'') | |
//ShowMessage(exst); | |
While exst do | |
begin | |
inc(i) | |
nRan := GenRan(itemCat,itemType); | |
exst := exists(nRan,''); | |
if i = 100 then begin | |
showMessage('Unable to generate unique number after 100 tries.'); | |
Exit; | |
end; | |
end; | |
SetPropertyFormula(AItem,'Item#',nRan); | |
SetPropertyAttribute(AItem,'Item#','userlocked','True'); | |
EndUpdate; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment