Conditional first run handler - titanium-mobile

I wonder if someone can help me with a simple script. I am not a good Titanium programmer yet, but I would like to be some day.
I need help with a script to check whether a certain settings file exists in the applicationDataDirectory.
I need a way to trigger this automatically in app.js:
If file with the with the name: "passWord.txt” does not(!) exist in applicationDataDirectory
then open window:
var w = Ti.UI.createWindow
I can´t find a event to check for this file and then open this window automatically. This will work as a “first run” event when the app is loaded the first time.

Try this
var file =Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,"passWord.txt”);
if ( file.exists() ) {
// do your work here.
}
For complete details Titanium.Filesystem.File.

Related

Firefox: run functions from Javascript through an extension

At the present I'm pasting a Javascript into the console of FF and I'm calling the functions from the console:
function fill (i){
if(i==1){
SINGLE_START();
}
else if(i==2){
DUAL_START();
}
else if(i==3){
INTEGRATED_START();
}
else{
alert("=======================\n Tool Filler\n=======================\n\n1 or 2");
}
}
It is used to scrape the content of the website and e.g. create a file or generate an email from certain parts of the website, e.g.:
function SINGLE_START(){
//Focus:
let d = $(document).activeElement.contentDocument.activeElement.contentDocument.activeElement.contentDocument;
etc.
I thougt, there could be a way to use it through an extension and so I installed Tampermonkey and saved the script as userscript within the extension. But than I have a problem that I'm not able to call the desired function from the script as I need it, not just start the script as the website loads.
Does anyone has an idea how to call the functions one by one from within Tampermonkey (or Greasemonkey), or any other extension?
Thanks in advance!
This is because Tampermonkey scripts run in isolated context. There are two kinds:
1. No special privilegies
If you're not using any special GM functions that are unlocked by #grant GM_doThisAndThat and instead you use #grant none, then what internally happens is something like this:
function TheScript() {
// Here is your script that you added to tampermonkey
}
TheScript();
This if you have a function there, it is only available in the script. You need to explicitly expose it to window context:
function fill (i){
... code here ...
}
window.myFill = fill;
Then in console you write myFill(42) and it will execute.
I would recommend that you avoid assigning fill to window since it's a name that could likely conflict with something, but that's up to you.
2. Special privilegies
If you're using some of the GM features, you need to add #grant unsafeWindow and then assign to unsafeWindow variable. Be careful what you expose like this, you don't want to allow the website to access any of the GM_function features as they could access your private data on other websites and your computer!

How to give a time delay in Visual Studio macros

Recently I updated my Visual Studio and start using the extention Macro Explorer.
I tried to use one of the sample macros "removes and sorts all", but I realized if I have a open documents, it doesn't run. So I closed all my open documents and try again this time it open all documents and close them but it doesn't work either.
The problem is the command executed before the document completely load. If there was a event or timer that can help wait until document load completely the problem will solve.
Here is my code. I marked where I want to add wait function:
function formatFile(file) {
dte.ExecuteCommand("View.SolutionExplorer");
if (file.Name.indexOf(".cs", file.Name.length - ".cs".length) !== -1) {
file.Open();
file.Document.Activate();
//here i want to wait for 1 second
dte.ExecuteCommand("Edit.RemoveAndSort");
file.Document.Save();
file.Document.Close();
}
}
I appreciate any sort of help.
Macro Explorer on GitHub: https://github.com/Microsoft/VS-Macros
According to the code snippet you shared in your original post and I also clone the project from GitHub, your code should be JavaScript code.
In JavaScript code, there has setTimeout() or setInterval() functions. For example, if you use setTimeout(), you need to move the code that you need to run after the pause into the setTimeout() callback.
Please modify your code as below.
function formatFile(file) {
dte.ExecuteCommand("View.SolutionExplorer");
if (file.Name.indexOf(".cs", file.Name.length - ".cs".length) !== -1) {
file.Open();
file.Document.Activate();
setTimeout(function () {
//move the code that you want to run after 1 second
dte.ExecuteCommand("Edit.RemoveAndSort");
file.Document.Save();
file.Document.Close();
}, 1000);
}
And there are lots of thread about sleep() in Javascript code. Please refer to:
What is the JavaScript version of sleep()?
JavaScript sleep/wait before continuing

Nativescript angular detect first run

Is there a way to detect first application run (on new build)? I can think of some custom solutions like a config file where I would write some flag after first run, but this wouldn't help me much. I'd like to show loading screen on first run, which would be different from the runs after.
In your imports:
import * as appSettings from "application-settings";
Then use:
appSettings.getBoolean("isFirst", true); - checks if its the first time loading up. If it is, "isFirst" won't exist and will therefore return true as , true) sets the return to true if the value isn't there.
You can then use:
appSettings.setBoolean("isFirst", false); - use this after logging in to set the value of "isFirst" for the next time

Calling checkCurrentDictionary() from addon crashes FF - why?

I'm tyring to call the method checkCurrentDictionary() of nsIEditorSpellCheck from within an add-on. The relevant code I use is:
var editorSpellCheck = Cc["#mozilla.org/editor/editorspellchecker;1"].createInstance(Ci.nsIEditorSpellCheck);
editorSpellCheck.checkCurrentDictionary();
This immediately crashes the Fx. What is going wrong here?
So this probably has something to do with the fact that nsIEditorSpellCheck is not a scriptable interface.
Basically, a scriptable interface is one that can be used from JavaScript.
If you want to access the spell check service you can do something like:
let editor = editableElement.editor;
if (!editor) {
let win = editableElement.ownerDocument.defaultView;
editor = win.QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIWebNavigation).
QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIEditingSession).
getEditorForWindow(win);
}
if (!editor)
throw new Error("Unable to find editor for element " + editableElement);
(The above is from http://dxr.mozilla.org/mozilla-central/source/editor/AsyncSpellCheckTestHelper.jsm which is MPL).
Then you can use the InlineSpellCheck.jsm to do some crazy stuff.
I'm not sure what you want to do though, so perhaps you should ask that more specific question as a new question.

How to Program xulrunner application to display output file or command line?

My question is in response to this article
https://developer.mozilla.org/En/How_to_check_the_security_state_of_an_XMLHTTPRequest_over_SSL
I have downloaded and configured the xulrunner the only problem I'm getting to run javascript given in the link to display it output. Using xulrunner i want to know how can i produce an output as a headerless command-line program not gui.
var httpRequest = Components.classes["#mozilla.org/xmlextr/xmlhttprequest;1"].createInstance();
// Disable alert popups on SSL error
httpRequest.mozBackgroundRequest = true;
httpRequest.open("GET", "https://developer.mozilla.org/", true);
httpRequest.onreadystatechange = function (aEvt) {
if (httpRequest.readyState == 4) {
// Print security state of request
dumpSecurityInfo(httpRequest.channel);
}
};
httpRequest.send(null);
In the above code taken from the same link i want to see the output of function on my command screen or even a writing the information to file would do.
Do i have to change something in *.xul file extension.? I'm new to using xulrunner some help would be very helpful for me.
To print something to the console you use the dump() function. If your code runs in the context of a window you will need to change browser.dom.window.dump.enabled preference to true. XPCOM components can simply call dump() without changing this preference.

Resources