Style script editor
Style script editor
This script is a library/tool/helper related to the properties of items (including containers) to get and set them. It supports strings, integers, booleans, boxes and event handlers.
Features:
– Get script code from item B that, when running in item A, will set the same properties of the item B. This can be useful when creating a script that creates an item, and you need that item with specific properties.
– The same as before but without copy/pasteing (it is saved in the script tag to apply).
– A class available to any script (you need to run this script at least once when the app loads) to perform any of those actions.
Instructions:
– Run the script from an item from long tap. The class will be initialized and a dialog with options will be shown.
– Choose get and save from item/container (if available) to display the code (the one that contains all the properties of the item/container)
– Choose apply to item/container (if available) to set the saved properties (from the previous run) to that item.
Important: although you can apply properties from items with different types, some of them won’t work and can make others don’t work too. This may change in future lighting versions.
]]>
< ![CDATA[
LL.bindClass(“android.app.AlertDialog”);
LL.bindClass(“android.content.DialogInterface”);
var script=LL.getCurrentScript();
var item=LL.getEvent().getItem();
var container=LL.getEvent().getContainer();
var saved=script.getTag();
if(self.StyleScript==undefined)
initialize();
var options=[“Get and save data from Item”,”Get and save data from Container”,”Apply saved data to Item”,”Apply saved data to Container”];
if(item==null) options[0]=options[2]=”-no item selected-“;
if(container==null) options[1]=options[3]=”-no container selected-“;
if(saved==null) options[2]=options[3]=”-no saved data-“;
dialog();
function dialog(){
showList(options,f_manageSelected,”Choose”);
}
function f_manageSelected(index){
switch(index){
case 0:
if(item!=null) saveAndShow(item);
else dialog();
break;
case 1:
if(container!=null) saveAndShow(container);
else dialog();
break;
case 2:
if(item!=null&&saved!=null) applyTo(item);
else dialog();
break;
case 3:
if(container!=null&&saved!=null) applyTo(container);
else dialog();
break;
}
}
function saveAndShow(item){
var properties= StyleScript.getProperties(item);
script.setTag(JSON.stringify(properties));
prompt(item,StyleScript.toCode(properties));
}
function applyTo(item){
var err=StyleScript.applyPropertiesToItem(JSON.parse(saved),item);
if(err!=””)alert(“Errors:\n”+err);
}
//function to display a List in a Popup, where the user can select one item. Adapted from Lukas Morawietz’s Multi tool script
function showList(items,onClickFunction,title){
var builder=new AlertDialog.Builder( LL.getContext() );
var listener=new DialogInterface.OnClickListener(){
onClick:function(dialog,which){
dialog.dismiss();
setTimeout(function(){onClickFunction(which);},0);
return true;
}
}
var cancelListener = new DialogInterface.OnClickListener(){
onClick:function(dialog,id){
dialog.cancel();
}
}
builder.setItems(items,listener);
builder.setCancelable(true);
builder.setTitle(title);
builder.setNegativeButton(“Cancel”,null);//it has a Cancel Button
builder.show();
}
/*
* #############################################
* ## Initialization of the StyleScript class ##
* #############################################
*/
function initialize(){
self.StyleScript={
/**
* Converts the object properties into a script representation
*/
toCode:function(data){
var string=”var p=item.getProperties().edit();\n”;
for(var i=0;i
switch(data.type[i]){
case “Box”:
string+=’var pbox=p.getBox(“‘+data.prop[i]+'”);\n’;
var box=data.value[i];
//sizes
for(var t=0;t
string+=’pbox.setSize(“‘+box.size.prop[t]+'”,’+box.size.value[t]+”);\n”;
}//end box for
//colors
for(var t=0;t
string+=’pbox.setColor(“‘+box.color.prop[t]+'”,”‘+box.color.state[t]+'”,’+box.color.value[t]+’);\n’;
}
//alignment
string+=’pbox.setAlignment(“‘+box.alignment.h+'”,”‘+box.alignment.v+'”);\n’;
break;
default:
string+=(string[string.length-2]==”;”?”p”:””)+”.set”+data.type[i]+'(“‘+data.prop[i]+'”,’+(data.type[i]==”String”?'”‘+data.value[i]+'”‘:data.value[i])+”)\n”;
}//end switch
}//end data.prop for
return string+(string[string.length-2]==”;”?”p”:””)+”.commit();”;
}//end toCode function
,
/**
* Returns the object properties of the item
*/
getProperties:function(item){
var data={prop:[],type:[],value:[]};
for(type in this.properties){
if(!(item==LL.getContainerById(99)&&type==”AppDrawer”)){
LL.bindClass(“net.pierrox.lightning_launcher.script.api.”+type);
if(self[type]==null) continue;//just in case
if(!(item instanceof self[type]) && type==”Container” ) continue;
}//end app drawer check
for(prop in this.properties[type]){
var proptype = this.properties[type][prop];
proptype=proptype==”int”?”Integer”:proptype.charAt(0).toUpperCase() + proptype.slice(1);
var value;
try{
value=item.getProperties()[“get”+proptype](prop);
}catch(e){continue};
switch(proptype){
case “Box”:
var boxdata={size:{prop:[],value:[]},color:{prop:[],value:[],state:[]},alignment:{h:0,v:0}};
for(var t=0;t
var sizeprop=this.boxsizes[t];
var sizevalue=value.getSize(sizeprop);
var index=boxdata.size.value.indexOf(sizevalue);
if(index!=-1){
boxdata.size.prop[index]+=”,”+sizeprop;
}else{
boxdata.size.prop.push(sizeprop);
boxdata.size.value.push(sizevalue);
}
}//end prop sizes
//colors
for(var t=0;t
var colorprop=this.boxcolors[t];
var tempdata={value:[],state:[]};
for(var tt=0;tt
var state=this.boxstates[tt];
var colorvalue = value.getColor(colorprop,state);
var index=tempdata.value.indexOf(colorvalue);
if(index!=-1){
tempdata.state[index]+=”,”+state;
}else{
tempdata.state.push(state);
tempdata.value.push(colorvalue);
}
}//end boxstate
//same color&state
for(var s=0;s
for(var ss=0;ss
if(boxdata.color.value[s]==tempdata.value[ss]
&& boxdata.color.state[s]==tempdata.state[ss]){
boxdata.color.prop[s]+=”,”+colorprop;
tempdata.value[ss]=null;
}
}//end doble for s ss
//different color|state
for(var ss=0;ss
if(tempdata.value[ss]==null) continue;
boxdata.color.prop.push(colorprop);
boxdata.color.value.push(tempdata.value[ss]);
boxdata.color.state.push(tempdata.state[ss]);
}
}//end prop colors
//alignment
boxdata.alignment.h=value.getAlignmentH();
boxdata.alignment.v=value.getAlignmentV();
data.value.push(boxdata);
break;
case “EventHandler”:
data.value.push([
“EventHandler.”+this.eventHandlerPairs[value.getAction()]
,value.getData()!=null?'”‘+value.getData()+'”‘:”null”
]);
//continue; //not supported yet
break;
default:
data.value.push(value);
}//end switch
data.prop.push(prop);
data.type.push(proptype);
}//end prop for
}//end types for
return data;
}//end getProperties function
,
/**
* Applies the properties (the object) to the item.
* Returns the properties that couldn’t be applied as string
*/
applyPropertiesToItem:function(properties,item){
var errors=””;
var p,pbox;
this.toCode(properties)
.replace(/\n\./g,”\np.”)
.replace(/var (.*)=/g,”$1=null;\n$1=”)
.split(“\n”)
.map(function(v){
eval(“function toEval(){“+v+”}”);
try{toEval()}catch(e){errors+=v+”\n”}
});
return errors;
}
,
/**
* Contains the list of properties of each type
*/
properties:{
“Container”:
{
“newOnGrid”:”boolean”,
“allowDualPosition”:”boolean”,
“gridPColumnMode”:”string”,
“gridPColumnNum”:”int”,
“gridPColumnSize”:”int”,
“gridPRowMode”:”string”,
“gridPRowNum”:”int”,
“gridPRowSize”:”int”,
“gridLColumnMode”:”string”,
“gridLColumnNum”:”int”,
“gridLColumnSize”:”int”,
“gridLRowMode”:”string”,
“gridLRowNum”:”int”,
“gridLRowSize”:”int”,
“gridPL”:”boolean”,
“gridLayoutModeHorizontalLineColor”:”int”,
“gridLayoutModeHorizontalLineThickness”:”float”,
“gridLayoutModeVerticalLineColor”:”int”,
“gridLayoutModeVerticalLineThickness”:”float”,
“gridAbove”:”boolean”,
“bgSystemWPScroll”:”boolean”,
“bgSystemWPWidth”:”int”,
“bgSystemWPHeight”:”int”,
“bgColor”:”int”,
“statusBarHide”:”boolean”,
“statusBarColor”:”int”,
“navigationBarColor”:”int”,
“statusBarOverlap”:”boolean”,
“navigationBarOverlap”:”boolean”,
“screenOrientation”:”string”,
“scrollingDirection”:”string”,
“overScrollMode”:”string”,
“noDiagonalScrolling”:”boolean”,
“pinchZoomEnable”:”boolean”,
“snapToPages”:”boolean”,
“fitDesktopToItems”:”boolean”,
“autoExit”:”boolean”,
“rearrangeItems”:”boolean”,
“swapItems”:”boolean”,
“freeModeSnap”:”string”,
“useDesktopSize”:”boolean”,
“noScrollLimit”:”boolean”,
“wrapX”:”boolean”,
“wrapY”:”boolean”,
“iconPack”:”string”,
“homeKey”:”EventHandler”,
“menuKey”:”EventHandler”,
“longMenuKey”:”EventHandler”,
“backKey”:”EventHandler”,
“longBackKey”:”EventHandler”,
“searchKey”:”EventHandler”,
“bgTap”:”EventHandler”,
“bgDoubleTap”:”EventHandler”,
“bgLongTap”:”EventHandler”,
“swipeLeft”:”EventHandler”,
“swipeRight”:”EventHandler”,
“swipeUp”:”EventHandler”,
“swipeDown”:”EventHandler”,
“swipe2Left”:”EventHandler”,
“swipe2Right”:”EventHandler”,
“swipe2Up”:”EventHandler”,
“swipe2Down”:”EventHandler”,
“orientationPortrait”:”EventHandler”,
“orientationLandscape”:”EventHandler”,
“posChanged”:”EventHandler”,
“load”:”EventHandler”,
“paused”:”EventHandler”,
“resumed”:”EventHandler”
},
“AppDrawer”:
{
“adHideActionBar”:”boolean”,
“adDisplayABOnScroll”:”boolean”,
“adDisplayedModes”:”int”,
“adActionBarTextColor”:”int”
},
“Item”:
{
“i.box”:”Box”,
“i.rotate”:”boolean”,
“i.selectionEffect”:”string”,
“i.selectionEffectMask”:”boolean”,
“i.enabled”:”boolean”,
“i.alpha”:”int”,
“i.pinMode”:”string”,
“i.filterTransformed”:”boolean”,
“i.onGrid”:”boolean”,
“i.hardwareAccelerated”:”boolean”,
“i.tap”:”EventHandler”,
“i.longTap”:”EventHandler”,
“i.swipeLeft”:”EventHandler”,
“i.swipeRight”:”EventHandler”,
“i.swipeUp”:”EventHandler”,
“i.swipeDown”:”EventHandler”,
“i.touch”:”EventHandler”,
“i.paused”:”EventHandler”,
“i.resumed”:”EventHandler”,
“i.itemAdded”:”EventHandler”,
“i.itemRemoved”:”EventHandler”
},
“Shortcut”:
{
“s.labelVisibility”:”boolean”,
“s.labelFontColor”:”int”,
“s.selectionColorLabel”:”int”,
“s.focusColorLabel”:”int”,
“s.labelFontSize”:”float”,
“s.labelFontTypeFace”:”string”,
“s.labelFontStyle”:”string”,
“s.labelMaxLines”:”int”,
“s.iconVisibility”:”boolean”,
“s.iconScale”:”float”,
“s.iconReflection”:”boolean”,
“s.iconReflectionOverlap”:”float”,
“s.iconReflectionSize”:”float”,
“s.iconReflectionScale”:”float”,
“s.iconFilter”:”boolean”,
“s.labelVsIconPosition”:”string”,
“s.labelVsIconMargin”:”int”,
“s.labelShadow”:”boolean”,
“s.labelShadowRadius”:”float”,
“s.labelShadowOffsetX”:”float”,
“s.labelShadowOffsetY”:”float”,
“s.labelShadowColor”:”int”,
“s.iconEffectScale”:”float”,
“s.iconColorFilter”:”int”
},
“Folder”:
{
“f.titleVisibility”:”boolean”,
“f.titleFontColor”:”int”,
“f.titleFontSize”:”float”,
“f.animationIn”:”string”,
“f.animationOut”:”string”,
“f.animFade”:”boolean”,
“f.iconStyle”:”string”,
“f.autoClose”:”boolean”,
“f.closeOther”:”boolean”,
“f.wAH”:”string”,
“f.wAV”:”string”,
“f.wX”:”int”,
“f.wY”:”int”,
“f.wW”:”int”,
“f.wH”:”int”,
“f.box”:”Box”,
“f.autoFindOrigin”:”boolean”
},
“PageIndicator”:
{
“p.style”:”string”,
“p.rawFormat”:”string”,
“p.dotsMarginX”:”int”,
“p.dotsMarginY”:”int”,
“p.dotsOuterRadius”:”int”,
“p.dotsInnerRadius”:”int”,
“p.dotsOuterStrokeWidth”:”int”,
“p.dotsOuterColor”:”int”,
“p.dotsInnerColor”:”int”,
“p.miniMapOutStrokeColor”:”int”,
“p.miniMapOutFillColor”:”int”,
“p.miniMapOutStrokeWidth”:”int”,
“p.miniMapInStrokeColor”:”int”,
“p.miniMapInFillColor”:”int”,
“p.miniMapInStrokeWidth”:”int”,
“p.lineBgWidth”:”int”,
“p.lineBgColor”:”int”,
“p.lineFgWidth”:”int”,
“p.lineFgColor”:”int”,
“p.lineGravity”:”string”
},
“CustomView”:
{
“v.onCreate”:”string”,
“v.onDestroy”:”string”
}
}
,
/**
* Contains the properties of all sizes of a box
*/
boxsizes:[“ml”,”mt”,”mr”,”mb”,”bl”,”bt”,”br”,”bb”,”pl”,”pt”,”pr”,”pb”]
,
/**
* Contains the properties of all colors of a box
*/
boxcolors:[“c”,”bl”,”bt”,”br”,”bb”]
,
/**
* Contains the states of the colors of a box
*/
boxstates:[“n”,”s”,”f”]
,
/**
* Contains the list of event handlers where the index is the id (initialized below)
*/
eventHandlerPairs:[]
};
//initialize
var keys= Object.keys(EventHandler);
for(var t=0;t
StyleScript.eventHandlerPairs[EventHandler[keys[t]]]=keys[t];
Android.makeNewToast(“StyleScript class initialized”,true).show();
}//end initialize function
]]>
< ![CDATA[
Hi TrianguloY, sorry to bother you but when I try to run this script, this dialog pops up:
At line 77: Java constructor for “android.app.AlertDialog$Builder” with arguments”” not found.
Help, please? 😀
]]>
< ![CDATA[
Donna Menes there were problems with Google+ and bold.
Try now. (Copy it again)
]]>
< ![CDATA[
Now working! Thanks TrianguloY! 🙂
]]>