Find full path with subfolder of macros

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
rstancuna
Posts: 23
Joined: Sun Jun 16, 2019 10:27 pm

Find full path with subfolder of macros

Post by rstancuna » Mon Sep 09, 2019 10:33 pm

Waterfox 56.2, iMacros V8.9.7.

I have several subfolders in the Macros folder, each with js macros.
Do we have any way of finding the full path with subfolder for current running file?
Waterfox 56.2, iMacros V8.9.7 on Windows
chivracq
Posts: 8716
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Find full path with subfolder of macros

Post by chivracq » Mon Sep 09, 2019 11:11 pm

rstancuna wrote:
Mon Sep 09, 2019 10:33 pm

Code: Select all

Waterfox 56.2, iMacros V8.9.7.
I have several subfolders in the Macros folder, each with js macros.
Do we have any way of finding the full path with subfolder for current running file?

OS is missing, from your FCI...?

- Do you "know" the Name (the Filename) of your '.js' Script already...? (i.e. can you hard-code it in your '.js' Script...?)
- And you only have 1 Level of Sub-Folders in your "main" 'Macros' Folder...?
- And you don't have Duplicate Filenames in different Sub-Folders...?
- And do you start your '.js' Script "manually" or from a '.BAT' File/Shortcut/OS-Scheduled Task...?
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
rstancuna
Posts: 23
Joined: Sun Jun 16, 2019 10:27 pm

Re: Find full path with subfolder of macros

Post by rstancuna » Mon Sep 09, 2019 11:47 pm

OS is Windows, didn't write OS since it doesn't really matter in this case.

To better explain:
C:\SomethingSomething\Macros\user1\login.js
C:\SomethingSomething\Macros\user2\login.js

I run macro via bookmark with:
imacros://run/?m=user2%5Clogin.js

login.js macros are pretty much the same, just different credentials so depending on which one will be run it will login with different credentials.
It will be run while already on the login page of a website, !URLCURRENT can't retrieve imacros://run/?m=user2\login.js .
I want to set the code so it will be the same in all files and know what credentials to use based on the subfolder.
How can my login.js know if it's in subfolder user1 or user2? Need a way to output the current subfolder or full path of the macro.
Waterfox 56.2, iMacros V8.9.7 on Windows
chivracq
Posts: 8716
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Find full path with subfolder of macros

Post by chivracq » Tue Sep 10, 2019 1:23 am

rstancuna wrote:
Mon Sep 09, 2019 11:47 pm
OS is Windows, didn't write OS since it doesn't really matter in this case.

To better explain:

Code: Select all

C:\SomethingSomething\Macros\user1\login.js
C:\SomethingSomething\Macros\user2\login.js
I run macro via bookmark with:

Code: Select all

imacros://run/?m=user2%5Clogin.js
login.js macros are pretty much the same, just different credentials so depending on which one will be run it will login with different credentials.
It will be run while already on the login page of a website, !URLCURRENT can't retrieve imacros://run/?m=user2\login.js .
I want to set the code so it will be the same in all files and know what credentials to use based on the subfolder.
How can my login.js know if it's in subfolder user1 or user2? Need a way to output the current subfolder or full path of the macro.

"... didn't write OS since it doesn't really matter in this case."
=> Pfff..., of course it does matter, there is "nothing" more OS-specific than File Paths, ah-ah...!
+ Using a '.js' Script, the single Forward/Backslashes from the original OS-Path need (or not) to be escaped/doubled/converted to Forward Slashes...
And I simply don't answer when FCI is not mentioned... :wink:

OK, I thought of several "Options" but none is the "real Winner"... :oops:

I reckon you have 2 different Bookmarks, I guess the easiest would be to just have one Var hard-coded at the beginning of each Script with the Name of the User, or have an Input 'PROMPT' where you can input a "1" or "2" for which User, or their Initial, or have your Bookmark instead load a local HTML Page in which you would have 2 Links or 2 Buttons to click with an embedded mini-Macro that would take the Username and further launch the 'login.js' Script...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
chivracq
Posts: 8716
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Find full path with subfolder of macros

Post by chivracq » Tue Sep 10, 2019 1:34 am

Hum, and that "1"/"2" in the 'PROMPT' could even be pre-filled in by Default with the "other" User from the one than ran last, by dumping some mini-'.TXT' File with the Name + Timestanp of the "previous" Run with which Username... And in most Cases you would just need to hit 'Enter' to accept the 'PROMPT... :idea:

Oh, but wait...!, the Mechanism I mentioned with an embedded Macro in a Link or Button, can also be done from your Bookmark, which instead of launching the corresponding 'login.js' Script, will first write to some mini-'.TXT' File which User to use and will then launch the 'login.js' which will retrieve that Info from the '.TXT' File... 8)
Last edited by chivracq on Tue Sep 10, 2019 1:44 am, edited 1 time in total.
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
rstancuna
Posts: 23
Joined: Sun Jun 16, 2019 10:27 pm

Re: Find full path with subfolder of macros

Post by rstancuna » Tue Sep 10, 2019 1:40 am

chivracq wrote:
Tue Sep 10, 2019 1:23 am
OK, I thought of several "Options" but none is the "real Winner"... :oops:

I reckon you have 2 different Bookmarks, I guess the easiest would be to just have one Var hard-coded at the beginning of each Script with the Name of the User, or have an Input 'PROMPT' where you can input a "1" or "2" for which User, or their Initial, or have your Bookmark instead load a local HTML Page in which you would have 2 Links or 2 Buttons to click with an embedded mini-Macro that would take the Username and further launch the 'login.js' Script...
Already have something similar, user1 has the bookmark imacros://run/?m=user1%5Clogin.js and user2 has the bookmark imacros://run/?m=user2%5Clogin.js and this is really the fastest since it needs the lowest amount of interaction by the user.

C:\SomethingSomething\Macros\user1\login.js - has user1 credentials
C:\SomethingSomething\Macros\user2\login.js - has user2 credentials

Just want to make a login.js that has both user1 and user2 credentials and username/password variables are set based on the subfolder that the macro is in. This way, updates on the code are faster since in the end it's the same file that i can copy paste in all subfolders instead of manually updating the code for each file in each subfolder.

There are websites where the users don't need to login with their personal credentials but said websites do have different accounts so in this case i have set it with a prompt asking to input 1/2/3 for which user but want to set the whole login.js to be identical and change some of the credentials based on the subfolder it's in.
Waterfox 56.2, iMacros V8.9.7 on Windows
rstancuna
Posts: 23
Joined: Sun Jun 16, 2019 10:27 pm

Re: Find full path with subfolder of macros

Post by rstancuna » Tue Sep 10, 2019 1:40 pm

Update.
The solution was actually quite simple. Each user has their own bookmark pointing to login.js so just changed the iMacros folder paths, changed Datasource to C:\Users\CURRENT_USER\Documents\iMacros\Datasources as it is initially. Then just:

Code: Select all

iimPlayCode("SET !EXTRACT {{!FOLDER_DATASOURCE}}");
currentuser=iimGetLastExtract().replace(/C:\\Users\\(.+).+Documents.+/,"$1"); //not really needed to use str.replace but i want to keep the variable clean
if (currentuser.match(/user1/)){
	user="aaa";
	pass="bbb";
}
else if (currentuser.match(/user2/)){
	user="aaa2";
	pass="bbb2";
}


Since i was just fixed on the idea that i need to know the full file path, it didn't even occur to me to just check the user from Datasource folder path. So before i realized this, ended up having to read 2 different local files but at least it opened up to some new things that i wanted to do in the past:

Bookmarks are set on PrefBar which allows setting hotkeys to run them.

This function is based on File I/O from Mozilla dev and will return the contents of a file:

Code: Select all

function readlocalfile(file_url){
	var FileUtils = Components.utils.import("resource://gre/modules/FileUtils.jsm").FileUtils;
	var file   = new FileUtils.File(file_url);
	var data = "";
	var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
				createInstance(Components.interfaces.nsIFileInputStream);
	var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
				createInstance(Components.interfaces.nsIConverterInputStream);
	fstream.init(file, -1, 0, 0);
	cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish
	str = {}
	let read = 0;
	do { 
		read = cstream.readString(0xffffffff, str);
		data += str.value;
	} while (read != 0);
	cstream.close();
	return data;
}
Then created a function that will return the default profile folder url:

Code: Select all

function getdefaultprofileurl(){
	iimPlayCode("SET !EXTRACT {{!FOLDER_DATASOURCE}}");
	url=iimGetLastExtract().replace(/Documents.+/,"")+"AppData\\Roaming\\Waterfox\\profiles.ini";
	profileurl=url.replace(/profiles.ini/,"")+readlocalfile(url).replace(/[\s\S]*Path=(Profiles\/.*\.default)[\s\S]*/,"$1").replace(/\//,"\\");
	return profileurl;
}
As previously stated, the bookmarks are set in PrefBar and saves it's preferences in prefbar.json in the profile folder. Won't post the whole file but it's something like this:

Code: Select all

[.........]
    "hkkey": "C",
    "hkkeycode": "",
    "hkmodifiers": "alt"
  },
  "prefbar:button:login": {
    "type": "link",
    "label": "login",
    "url": "imacros://run/?m=user1%5Clogin.js",
    "hkkey": "W",
    "hkkeycode": "",
    "hkmodifiers": "alt"
  },
  "prefbar:button:Somemacro": {
    "type": "link",
    "label": "Scrub",
    "url": "imacros://run/?m=somemacro.js",
    "hkkey": "A",
    "hkkeycode": "",
    "hkmodifiers": "accel,alt,shift"
  },
  "prefbar:button:Anothermacro": {
    "type": "link",
    "label": "check",
    "url": "imacros://run/?m=other%5Canothermacro.js",
    "hkkey": "F",
    "hkkeycode": "",
    "hkmodifiers": "accel,alt,shift"
  },
  "prefbar:button:2": {
    "type": "link",
[.........]
Now just had to change the url returned by getdefaultprofileurl() to point to prefbar.json , read that file as well and parse the JSON to get the login bookmark url:

Code: Select all

prefbarfileurl=getdefaultprofileurl()+"\\prefbar.json";

getprefbarjson=readlocalfile(prefbarfileurl);
getprefbarjson=JSON.parse(getprefbarjson);

prefbaroptions=Object.entries(getprefbarjson);

for (var i=0; i < prefbaroptions.length; i++){
	if (prefbaroptions[i][0].match(/prefbar:button:login/)){
		currentuser = prefbaroptions[i][1].url.replace(/imacros.+m=(.+)%5Clogin.+/,"$1");
		break;
	}
}
currentuser would be user1
Waterfox 56.2, iMacros V8.9.7 on Windows
access2vivek
Posts: 50
Joined: Wed Dec 20, 2017 10:00 am
Location: New Delhi, India
Contact:

Re: Find full path with subfolder of macros

Post by access2vivek » Wed Oct 16, 2019 8:50 am

As far as I have been trying to understand the problem, I think this is the problem statement: -

You have multiple user profiles created on Firefox. Let's say the profile names are "user1" and "user2".
You have written a macro in js which would allow you to log in to a specific website.
You want a system via which the system would know which user profile is being used to run the current script so that there can be different username and password based on the profile name.

Earlier, you have tried to create separate macros for each user and you were running a different macro for different user. However, it would be cumbersome to scale this process for many users.

I tried to understand the solution you shared earlier, however, If the problem is the one mentioned above, here is a simple solution that I propose.

Code: Select all

function getCurrentProfile()
{
	var code="CODE:";
	code+="URL GOTO=about:profiles\n";
	code+="TAG POS=1 TYPE=H3 ATTR=TXT:This<SP>is<SP>the<SP>profile*\n";
	code+="TAG POS=R-1 TYPE=H2 ATTR=* EXTRACT=TXT";
	iimPlay(code);
	return iimGetLastExtract().replace("Profile: ","");
}
The above method simply goes to the "about:profiles" page and finds the profile in use. You can then use this information to code the username and password in the same JS file.

Therefore, you should be able to save information about all the users in a single file. I hope it helps :-)
Robust iMacros development with quick turnaround time. Drop an email at "access2content@gmail.com" with your project details.
rstancuna
Posts: 23
Joined: Sun Jun 16, 2019 10:27 pm

Re: Find full path with subfolder of macros

Post by rstancuna » Fri Oct 18, 2019 1:05 am

I have several js macros that have a lot of functions and actions based on current url and clipboard information, all being tied to hotkeys to be executed fast and some even run from other js macros.
Your solution would work if you use 1 macro and just need to check at the start of it but if you would have a configuration like i stated above and for many users it wouldn't be useful.
This is an example how my macros are currently set:
tab url: example.com/login
hotkey1: run macro1.js which will do login for user1 and different login for user2
----
tab url: example.com/home
hotkey1: run macro2.js which will do something for user1 and somethingelse for user2
hotkey2: run macro3.js which will do something regardless of user
hotkey3: run macro4.js which based on clipboardinfo1 will do something for user1 and something else for all other users, based on clipboardinfo2 will do totally something else
hotkey4: run macro5.js which based on clipboardinfo1 will do somethingsomethingelse for user2 and someotherthing else for all other users
Sometimes, may even need to run hotkey1 to view something then hotkey2 to check something then hotkey1 again to do something. Can't combine all into 1 macro since i would need to create quite a lot of prompts asking the user what they want to do.

Going by your suggestion, i would have to leave my current page, go to about:profiles and back to my page (or open tab, close tab). Hotkeys and actions are triggered quite frequently so having to go to about:profiles each time will make things very very slow.

Tho, it's a good suggestion for those who wouldn't have my use case.
Waterfox 56.2, iMacros V8.9.7 on Windows
Post Reply