Steven Morgan Posted June 22, 2021 Share Posted June 22, 2021 (edited) Been digging around but haven't found the best way to accomplish this, wondering if anybody has an idea. Basic goal: Find a replace style symbol swap, but only swapping part of the symbol name. i.e: OriginalSymbol = Video Tile 9mm 2x3 NewSymbol = Video Tile 12mm 2x3 However the "2x3" could be any range of variables, I just need to swap the "mm" type (which are all symbols in the resource browser). My original thought was to SubString each symbol and then Concat them back together but am having trouble with the exact syntax. Working off the script below. Thanks! PROCEDURE ReplaceSymbols; { 03 Jun 2017 - Raymond J Mullin. } CONST NewSymName = '9mm'; VAR NewSymDefH :Handle; function SwapSym(H :Handle) :Boolean; { swap symbol instance H with NewSymDefH } Begin if (GetTypeN(H) = 15) then SetHDef(H, NewSymDefH); { swap Symbol Definitions } End; BEGIN NewSymDefH := GetObject(NewSymName); { Handle to new symbol name } if (NewSymDefH <> nil) then ForEachObjectInLayer(SwapSym, 2, 0, 4) { Selected, Shallow, Editable Layers } else AlrtDialog(concat('Symbol "', NewSymName, '" does not exist. Check spelling.')); END; Run(ReplaceSymbols); Edited June 22, 2021 by stevenmorgan94 Quote Link to comment
C. Andrew Dunning Posted June 22, 2021 Share Posted June 22, 2021 Have you tried the built-in Batch Rename command? Quote Link to comment
Steven Morgan Posted June 22, 2021 Author Share Posted June 22, 2021 (edited) Yes Batch Rename works great, I just need to replicate it via script as part of a larger project (plugin) I'm working on. Edit: Actually I lied, I think Batch Rename just renames the symbols in the Resource Browser. I need it to actually replace the symbols and their attached records. Edited June 22, 2021 by stevenmorgan94 1 Quote Link to comment
Pat Stanford Posted June 22, 2021 Share Posted June 22, 2021 So you have 9mm symbols inserted in the drawing. You want to be able to step through those symbols and replace them with the 12mm version. 1. Do the 12mm symbols already exist or do you need to create them? 2. Are the symbol names all completely consistent? ie. does the 12mm part always occur in the same location (number of character or number of spaces from the beginning of the name)? 3. Are you always going to go from the 9mm to the 12mm? Or are there times you would want to go from 12 to 9? That makes it more complicated as you will need to do more work figuring our how to munge the string as the lengths will change. 4. Would you be willing to consider renaming all of the 9mm symbols to 09mm so that the changing part of the string is always the same length? 5. The more you tell us about what you are really trying to do the better advise you will get. The following is intended as humor ;-) Quote The right way to get useful and valid internet help is NOT to say "Hi everyone, how do I fuzzard a biple?", but "Hi everyone, I have an urgent need to wax my bazard. I already possess a dry biple, which I'm thinking could be fuzzarded into a slimy zagnoggle that should then serve me as an adequate wax. If anyone can suggest a more appropriate way I could achieve my thickly waxed bazard, or else tell me how to perform the aforementioned biple-to-zagnoggle conversion, I would deeply axspengunliate it.” - has, AppleScript-Users list, January 13, 2017 Quote Link to comment
Steven Morgan Posted June 22, 2021 Author Share Posted June 22, 2021 (edited) Hey Pat, 1. Do the 12mm symbols already exist or do you need to create them? All symbols already exist. 2. Are the symbol names all completely consistent? ie. does the 12mm part always occur in the same location (number of character or number of spaces from the beginning of the name)? Naming conventions are the same across all pixel pitches. Video Tile 9mm 2x1 Video Tile 12mm 2x1 Video Tile 6mm 2x1, etc 3. Are you always going to go from the 9mm to the 12mm? Or are there times you would want to go from 12 to 9? That makes it more complicated as you will need to do more work figuring our how to munge the string as the lengths will change. It will change, my plan was to do a PullDownMenu with all of the options that store it to a String. The clunky idea I had was to substring out the original symbol name ([1]Video [2]Tile [3]9mm [4]2x1) and swap the [3] string for the new pixel pitch. Then Concat them all back together. 4. Would you be willing to consider renaming all of the 9mm symbols to 09mm so that the changing part of the string is always the same length? That would not be a problem. Clearly very new to scripting so if changing the entire approach would be a better option and you'd have a direction to point me in I can do some digging. Maybe storing all sets of options to different arrays and swapping between those? array06[Video Tile 6mm 2x1, Video Tile 6mm 2x2] array09[Video Tile 9mm 2x1, Video Tile 9mm 2x2] array12[Video Tile 12mm 2x1, Video Tile 12mm 2x2] swap(array06[1], array09[1]) ? Thanks so much. Edited June 22, 2021 by stevenmorgan94 Quote Link to comment
Pat Stanford Posted June 23, 2021 Share Posted June 23, 2021 Two methods in this script. Both get you the "replacement" symbol name you are looking for. I leave it as an exercise for the reader to pick the best option for your case, enhance the entry of the string to replace with, and to actually do the symbol replacement ;-) Procedure Test; {Quick and dirty demo of manipulating strings in VW2021} Var H1:Handle; S1, S2, S3, S4, S5: String; SS1, SS2, SS3, SS4: String; N1, N2: Integer; Begin H1:=FSActLayer; If GetTypeN(H1)=15 then Begin S1:=StrDialog('Enter Text To Replace', '9mm'); S2:=StrDialog('Enter Text to Replace With', '12mm'); S3:=GetSymName(H1); AlrtDialog(S3); {Method 1 Break into all substrings} SS1:=SubString(S3,' ',1); SS2:=SubString(S3,' ',2); SS3:=SubString(S3,' ',3); SS4:=SubString(S3,' ',4); S4:=Concat(SS1,' ',SS2,' ',S2,' ',SS4); AlrtDialog(Concat('S4: ',S4)); {Method 2 Find position of substring to replace and use Copy to get } {other parts of the string.} N1:=Pos(S1, S3); N2:=Len(S1); S5:=Concat(Copy(S3,1,N1-1), S2, Copy(S3,N1+N2,255)); AlrtDialog(Concat('S5: ',S5)); End; End; Run(Test); Procedure Test; {Quick and dirty demo of manipulating strings in VW2021} Var H1:Handle; S1, S2, S3, S4, S5: String; SS1, SS2, SS3, SS4: String; N1, N2: Integer; Begin H1:=FSActLayer; If GetTypeN(H1)=15 then Begin S1:=StrDialog('Enter Text To Replace', '9mm'); S2:=StrDialog('Enter Text to Replace With', '12mm'); S3:=GetSymName(H1); AlrtDialog(S3); {Method 1 Break into all substrings} SS1:=SubString(S3,' ',1); SS2:=SubString(S3,' ',2); SS3:=SubString(S3,' ',3); SS4:=SubString(S3,' ',4); S4:=Concat(SS1,' ',SS2,' ',S2,' ',SS4); AlrtDialog(Concat('S4: ',S4)); {Method 2 Find position of substring to replace and use Copy to get } {other parts of the string.} N1:=Pos(S1, S3); N2:=Len(S1); S5:=Concat(Copy(S3,1,N1-1), S2, Copy(S3,N1+N2,255)); AlrtDialog(Concat('S5: ',S5)); End; End; Run(Test); 1 Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.