how to use macro to make my code
-
I have been thinking, how i cancreate macro that would put information i need for code in following syntax
switch n case 1 country=af coutry_word=Afghanistan country_flag=https://imgur.com/yRbLG3B country_code_number=+324 break; case 2 ...
i created macro and used column edit, to make numbers go from 1 - 230 (suppose i need to input 230 cases, for 230 countries)
depending od case (number), it would set up few variables, abbreaviation for country, country full name, country flag (which is a link which is then later parsed in code…) and country calling numberand my trouble is, that i can create numbers to go from 1 - 230, and even add case (using column edit), and also i managed to add this whole set of variables to every 230 instances of this via recorded macro, but i left empty after =
But what i don’t know how to do is how to put unique data for all those variables after = (so, there would be 920 unique data to be putted into that).
But the thing is that i managed to get all link files in order for images , and also have all country abbreviations in order, and full country names in order, and also country calling codes in order.
But only thing left is that i don’t want to put each one of those 920 lines into these respective variables manually.
I tried with macro, but it doesn’t countinue on next line, but do only how much i record (it do it right, but it doesn’t go to the next line, so it doesnt even go at the end of file)
How i can do this.
And also, if notepad++ can’t do this, can you reccomend me some other editor which is suitable for these kinds of repetitive tasks.
Because i often encounter this much need to do (to clean something so it can fit into code etc…), but in fact it can be automated, i just don’t know how. -
@igor-lerinc said in how to use macro to make my code:
Because i often encounter this much need to do (to clean something so it can fit into code etc…), but in fact it can be automated, i just don’t know how.
Probably one of the scripting plugins would be the thing to use here, but the learning curve is fairly steep.
-
@igor-lerinc
I you don’t mind installing the PythonScript plugin, you could execute a script that roughly looks like thistextToInsert = ['country= ', 'coutry_word=', 'country_flag=', 'country_code_number='] editor.documentStart() for line in range(0, 920): text = textToInsert[line%4] pos = editor.getCurrentPos() editor.insertText(pos, text ) editor.lineDown()
Here I assume that you have an existing file of 920 lines (=230*4) which describe for each of the 230 countries the same kind of 4 pieces of info.
-
@paul-wormer
As first line you needfrom Npp import *
. If some of the country names contain UTF-8 codes you need# -*- coding: utf-8 -*-
on top of your script as well. -
@igor-lerinc The suggestion by the other guys to use scripting is probably the best approach especially if you have to rebuild the file (or others like it) at least somewhat often.
But if you’re not inclined to learn a scripting language in the short run, and especially if you already have some spreadsheet skill, this task can be achieved using something like OpenOffice Calc or Excel. The process would be along these lines:
- make a column of ascending numbers (Fill… Down…)
- optionally, make columns with your static text (
case
,country=
and the like) - import your 4 x 230 item lists as columns
- move the columns around, so you have one row per record with most or all the text that goes into a record
- optionally, use functions like CONCATENATE to do some massaging, that is, merge columns, so there’s less work to do in Npp.
- save as csv (unless your CONCAT-massaging is so complete you’re down to a single column, in which case, save as txt)
- open in Npp
- use a Find & Replace to break each row into a multiline record (by substituting in newlines for commas or any other record separator chars you used such as whitespace), including the adding of leading whitespace, empty lines, etc
And after typing the above, it occurs to me that you could probably do it all in Npp using Npp’s native column copy/paste operations, which you already are good at using. That is, do what you already did, except build it as one line per record, adding static text columns, then, variable data columns, then the next static text column, etc. You may wish to put in anchoring text columns to help manage the final formatting:
case 1 BREAK1 country=af BREAK2 coutry_word=Afghanistan BREAK3 country_flag=https://imgur.com/yRbLG3B BREAK4 country_code_number=+324 BREAK5 break;
Finally, as before, transform each line into multi-line records with the desired whitespace using Find & Replace.
You can also automate some of this with macros, since column pastes can be performed within a macro.
-
@neil-schipper said in how to use macro to make my code:
you could probably do it all in Npp
But the advantage of using a spreadsheet is that it also provides a convenient way of maintaining (adding/removing countries, modifying specific fields, and so on), and version tracking the source data that is used to create the records.
-
@paul-wormer said in how to use macro to make my code:
you could execute a script that roughly looks like this
I gave it a whirl and upon execution I got one really long line generated (12420 characters), that pretty much looks like this, over and over on that one line:
country= coutry_word=country_flag=country_code_number=
I’m not sure how that gets close to what the OP wants.
Of course, I found it difficult to understand what the OP does want, contributing to why I didn’t offer any ideas beyond the possibility of using a scripting plugin…
-
country="ad" country="ae" country="af" country="ag" country="ai" country="al" country="am" country="ao" country="aq" country="ar" country="as" country_code_number="+376" country_code_number="+971" country_code_number="+93" country_code_number="+1" country_code_number="+1" country_code_number="+355" country_code_number="+374" country_code_number="+244" country_code_number="+672" country_code_number="+54" country_code_number="+1" country_flag=R.drawable.flag_andorra; country_flag=R.drawable.flag_uae; country_flag=R.drawable.flag_afghanistan; country_flag=R.drawable.flag_antigua_and_barbuda; country_flag=R.drawable.flag_anguilla; coutry_word="Andorra" coutry_word="United Arab Emirates (UAE)" coutry_word="Afghanistan" coutry_word="Antigua and Barbuda" coutry_word="Anguilla" coutry_word="Albania" coutry_word="Armenia" coutry_word="Angola" coutry_word="Antarctica"
Here is what i managed to get using some regex (so assigning variable is done this way), but i still need to put them in syntax of switch case
case n country="ad" coutry_word="Andorra" country_flag=R.drawable.flag_andorra; country_code_number="+376" break; case n+1
So because i already assigned variable names, all i’m left to do is to put those variables into switch case syntax
how these can be done?
as it looks i’ve done those 920 times, i only need to wrap it into corresponding…
yes, and they are sorted this way accrodingly. (if it’s first for country name, it means first line in country number would mean for that first line country name, same is for abbrv, and it repeats to end) -
@igor-lerinc said in how to use macro to make my code:
i still need to put them in syntax of switch case
It would be good if you showed 2 or 3 record sample “before” and “after” text boxes so we can see the exact input - output relationship you want.
I have trouble understanding your text. Here’s an easy experiment: type up your text in your language (try using the simplest and most common words and grammar and sentence structure that you can while still getting your intentions across). Then run that through a translator and provide us with the output.
-
@neil-schipper said in how to use macro to make my code:
I have trouble understanding your text.
Yep, me too.
-
@alan-kilborn I appreciate the validation (even though sometimes I’m genuinely thick).
-
@neil-schipper
this is syntax i need. there’s nothing to directly compile, as this is supposed to just insert appropriate variables depending on the input n (user inputs number 2 and variables those defined would be loaded and processed in code further)BEFORE
country="ad" country="ae" country="af" country="ag" country="ai" country="al" country="am" country="ao" country="aq" country="ar" country="as" country_code_number="+376" country_code_number="+971" country_code_number="+93" country_code_number="+1" country_code_number="+1" country_code_number="+355" country_code_number="+374" country_code_number="+244" country_code_number="+672" country_code_number="+54" country_code_number="+1" country_flag=R.drawable.flag_andorra; country_flag=R.drawable.flag_uae; country_flag=R.drawable.flag_afghanistan; country_flag=R.drawable.flag_antigua_and_barbuda; country_flag=R.drawable.flag_anguilla; coutry_word="Andorra" coutry_word="United Arab Emirates (UAE)" coutry_word="Afghanistan" coutry_word="Antigua and Barbuda" coutry_word="Anguilla" coutry_word="Albania" coutry_word="Armenia" coutry_word="Angola" coutry_word="Antarctica"
AFTER
switch (n) { case 1: country="ad" country_code_number="+376" country_flag=R.drawable.flag_andorra; coutry_word="Andorra" break; case 2: country="ae" country_code_number="+971" country_flag=R.drawable.flag_uae; coutry_word="United Arab Emirates (UAE)" break; case 3: country="af" country_code_number="+93" country_flag=R.drawable.flag_afghanistan; coutry_word="Afghanistan" break; default: console.log(`Nothing here`); }
and to do that for all these 230 possible cases
(in fact it doesnt receive number directly from user, but just to explain it more vividly) -
@igor-lerinc said in how to use macro to make my code:
there’s nothing to directly compile, as this is supposed to just insert appropriate variables depending on the input n (user inputs number 2 and variables those defined would be loaded and processed in code further)
I know you’re not trying to be cruel, but I am experiencing this as pain. Ouch! Can we try the russian-english translation method?
And if you are talking about what the switch statement is being used for in your application, that is not relevant to the text manipulation problem you want help with.
Your sample before and after text is helpful.
Is it a requirement that the process handle sublists of different sizes as in your sample (10 country but 5 country_flag)?
What determines that the process result in 3 fully formed output records even though none of the sublists consist of 3 items?
-
First, further to my prior post:
- your sample AFTER text looks like C code, but some lines have a semicolon and others do not
- coutry_word is probably a typo.
Now, I composed a macro that, using “Run a macro multiple times…” (and specifying 4 times), turns this:
1 2 3 4 country="ad" country="ae" country="af" country="ag" country_code_number="+376" country_code_number="+971" country_code_number="+93" country_code_number="+1" country_flag=R.drawable.flag_andorra; country_flag=R.drawable.flag_uae; country_flag=R.drawable.flag_afghanistan; country_flag=R.drawable.flag_antigua_and_barbuda; coutry_word="Andorra" coutry_word="United Arab Emirates (UAE)" coutry_word="Afghanistan" coutry_word="Antigua and Barbuda"
into this:
1 2 3 4 country="ad" country="ae" country="af" country="ag" country_code_number="+376" country_code_number="+971" country_code_number="+93" country_code_number="+1" country_flag=R.drawable.flag_andorra; country_flag=R.drawable.flag_uae; country_flag=R.drawable.flag_afghanistan; country_flag=R.drawable.flag_antigua_and_barbuda; coutry_word="Andorra" coutry_word="United Arab Emirates (UAE)" coutry_word="Afghanistan" coutry_word="Antigua and Barbuda" 1 country="ad" country_code_number="+376" country_flag=R.drawable.flag_andorra; coutry_word="Andorra" 2 country="ae" country_code_number="+971" country_flag=R.drawable.flag_uae; coutry_word="United Arab Emirates (UAE)" 3 country="af" country_code_number="+93" country_flag=R.drawable.flag_afghanistan; coutry_word="Afghanistan" 4 country="ag" country_code_number="+1" country_flag=R.drawable.flag_antigua_and_barbuda; coutry_word="Antigua and Barbuda"
Note that the macro:
- relies on there being exactly 5 groups of exactly 4 lines with exactly one line between groups
- appends the new text to the end, and leaves the original text untouched
It would be trivial to enhance the macro so that output groups also have whatever leading whitespace, case, break, semicolons, etc. are desired.
I don’t know if it would meet your needs because I still don’t fully understand your needs.
I had trouble finding the actions that got it to work. The Ctl-Shift-down operation did not work as I had hoped (in macro playback). However, using bookmark actions Ctl-F2 and F2, along with other conventional select, copy, paste actions, I was able to get the result I wanted.
-
-
-
Please do not delete your topic after there have been replies. We might not have a way to mark a topic as “answered”, but deleting the topic is not the right way to “end” the question. Just say, “thanks, that worked” or something to that effect, and the conversation will end naturally.
-
-
@alan-kilborn
You should have used an existing file with 920 lines.editor.lineDown()
does not add a new line. -
@paul-wormer
My solution was useless anyway now that I saw his BEFORE and AFTER. I, too, misunderstood his problem. -
I don’t think you have a handle on how to reply to postings here, evidence:
So you are telling me that I “should have used an existing file with 920 lines”. Well, I don’t know what that means, because I haven’t contributed in that way to this thread.
Also, you’re replying to yourself; this can be done, but try to do it only if it makes sense.
-
@alan-kilborn said in how to use macro to make my code:
II gave it a whirl and upon execution I got one really long line generated (12420 characters), that pretty much looks like this, over and over on that one line:
country= coutry_word=country_flag=country_code_number=
I’m not sure how that gets close to what the OP wants.
I am sorry that I don’t understand the mechanics of this particular forum. But my answer about
editor.lineDown()
was about your comment above. -
@paul-wormer said in how to use macro to make my code:
I am sorry that I don’t understand the mechanics of this particular forum. But my answer about editor.lineDown() was about your comment above.
Ah, apologies.
And you did say earlier:Here I assume that you have an existing file of 920 lines
So I missed that.
Again, sorry.
Other thoughts:
editor.lineDown()
isn’t really a great way to script something like this. It’s too “macro”-ish, and scripts give you the power to work with more of a data set rather than “careting” around a document as in a macro approach.But…it is hard to say more, because I really don’t have a great grasp on the problem statement, as IMO the OP hasn’t made it very clear (and the longer that state persists, the less interested I am).