StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
szhielelp  
#1 Posted : Sunday, January 24, 2021 1:41:54 PM(UTC)
szhielelp

Rank: Newbie

Reputation:

Groups: Approved
Joined: 1/24/2021(UTC)
Posts: 2
China

Was thanked: 1 time(s) in 1 post(s)
im a heavy user of StrokePlus for years.

I have many hotkeys which have similar pattern of scripts.

Is it possibly to create a macro (or something like a function) then I can reuse my scripts across multiple hotkeys?

Thanks
thanks 1 user thanked szhielelp for this useful post.
randomConstant on 11/11/2021(UTC)
Rob  
#2 Posted : Sunday, January 24, 2021 3:38:05 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
Sure, there are a few ways to accomplish this.

1. Macros

In the example below, I've created a category in Macros named Functions. In that category, I've created two macros named Test1 and Test2:

Macro: Functions > Test1:
Code:
sp.MessageBox("Test1", "Test1");

Macro: Functions > Test2:
Code:
sp.MessageBox("Test2", "Test2");

Then in a hot key or action script, you can pull in those scripts and use the JavaScript function eval to execute them.
Code:
eval(sp.GetMacroScript("Functions", "Test1"));

eval(sp.GetMacroScript("Functions", "Test2"));

Just make sure to be consider variable scope within eval (research via Google if needed), but for most situations it will behave as you would expect.



2. Global Actions > Load/Unload > Load

The Load script is run in each script engine and can be used to define global functions or variables available to all scripts.
In this example, I've added these functions in the Load script tab:
Code:
function globalTest1() {
    sp.MessageBox("Test1", "Test1");
}

function globalTest2() {
    sp.MessageBox("Test2", "Test2");
}

Now from a hot key or action script, they can be called anywhere:
Code:
globalTest1();

globalTest2();
 




3. Options > Advanced > External Script

You can select an external js file to hold any code, variables, or functions you want and have them available to all scripts.
This behaves exactly the same as the Load scripts, but may be helpful for someone who wants to manage the scripts in an external editor, for example.
Code:
//C:\Temp\externalscripts.js
function externalTest1() {
    sp.MessageBox("Test1", "Test1");
}

function externalTest2() {
    sp.MessageBox("Test2", "Test2");
}

Then in hot key or action script:
Code:
externalTest1();

externalTest2();
thanks 3 users thanked Rob for this useful post.
randomConstant on 11/11/2021(UTC), jay99995 on 8/25/2022(UTC), emr on 4/17/2024(UTC)
szhielelp  
#3 Posted : Friday, January 29, 2021 2:23:06 PM(UTC)
szhielelp

Rank: Newbie

Reputation:

Groups: Approved
Joined: 1/24/2021(UTC)
Posts: 2
China

Was thanked: 1 time(s) in 1 post(s)
Cool! This really make my life earlier.

Thanks for the detailed guidance. :)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.