User Tools

Site Tools


script_open_url

====== About the script ====== * Purpose : Script to create/manage shortcuts to open an url. * Author : TrianguloY * Link: https://community.lightninglauncher.com/viewtopic.php?f=14&t=24 ====== How to use the script ====== A script to open an url from a shortcut can be made with 3 lines of code. This script does that but also allows for multiple features regarding the creation of those shortcuts. To be precise the script allows you to: 1) If you run the script with an url as data (via lightning action->run script) that url will be opened. This can be used to open urls on gesture actions (long tap, swipe, stop points...) without explicitly create the shortcut. Note: the script must exist to open the url, but the url can be changed easily from the action configuration screen. 2) If you run the script from an item, you can change the intent of the item so that it will open the url (replacing any existing intent). Note: once the intent is set the script is no longer needed, but to change the url you need to edit the intent again. 3) If you run the script from a container, you can create a new shortcut to open a url. This is the same as creating a new empty item and changing the intent with option 2 (the script is not needed for the item to work, but to change the url you need to edit the intent) 4) You can also run the script to launch manually a url...if for some reason you don't want to open your browser directly :P ====== Script code ====== <sxh javascript> //check data, if found open that url var data = getEvent().getData(); if(data != null){ open(data); return; } //no data, check item. if found replace intent var i = getEvent().getItem(); if(i != null && confirm("Do you want to set the intent of the item '"+i+"' to open a url? Warning! this will replace the existing intent!!!")){ setIntent(i); return; } //no data nor item, check container. if found ask to create item var c = getEvent().getContainer(); if(c != null && confirm("Do you want to create a new item to open a url when clicked?")){ createItem(c); return; } //no data nor item nor container, just ask and open if(confirm("Do you want to manually open a url? (for script testing purposes)")){ manual(); return; } //nothing toast("No option selected :(") //Opens the url in a browser //Shows toast on error function open(url){ if( !getBackgroundScreen().startActivity(createIntent(url)) ){ toast("The url '"+url+"' could not be opened."); } } //returns an intent to open the url function createIntent(url){ var intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); return intent; } //asks the user to enter a url //returns the user input (null if cancelled) function askUrl(){ return prompt("Write the url you want to open","https://"); } //asks for a url //opens it (nothing if cancelled) function manual(){ var url = askUrl(); if(url == null) return; open(url); } //asks the user a url //sets the intent of item i to open that url (nothing if cancelled) function setIntent(i){ var url = askUrl(); if(url == null) return; i.setIntent(createIntent(url)); toast("item modified"); } //asks the user a url and a name //creates a new item in the container c to open that url (nothing if cancelled) function createItem(c){ var url = askUrl(); if(url == undefined) return; var name = prompt("Name of item?","Open "+url); if(name == undefined) return; var px = getEvent().getTouchX(); var py = getEvent().getTouchY(); var i = c.addShortcut(name,createIntent(url),px,py); i.setDefaultIcon(Image.createTextIcon("Z",100,Color.GRAY,Color.TRANSPARENT,null));//"Z" is the web icon toast("item added"); } //Shows a toast with the message m function toast(m){ Toast.makeText(getBackgroundScreen().getContext(),m,Toast.LENGTH_LONG).show() } </sxh>

script_open_url.txt · Last modified: 2019/01/30 16:00 by trianguloy