How can we create a NSWindow in Javascript for automation (mac JXA)? - javascript-automation

I would like to use JavaScript for automation (mac JXA) to make a NSWindow and show it to the user.
I tried following this example, but it crashes when I run it in the Script Editor app.
Then I tried to make my own using barebones. As a start I just want a window with a title in it.
ObjC.import("Cocoa")
var window = $.NSWindow.alloc.init
window.setContentSize($.NSMakeSize(500, 500))
window.title = "hello world"
window.makeKeyAndOrderFront(window)
When I try to run this in Script Editor the app crashes right away. It appears to be crashing when I try to initialize the NSWindow Object.
Am I doing something wrong?

I followed this guide and they say you have to save it as an application and run it as an application.
Heres a clip where they mention it in the article.

Related

PHPstorm console input not working during debug sessions

I'm using a PHP script which expects user input from a command like fgets(STDIN). The problem is it no longer works in the newest version of PHPStorm (10).
The same works when I run it directly (without debugger enabled) and anything I enter in the console is sent to the script (on direct run).
But during a debug session, when I try to input text at the script's prompt, it does not go to the script. My best guess is that the new REPL feature is overriding user input in console during debugging. I say this because pressing the UP/DOWN arrows opens up a popup with all PHP function names.
It used to work correctly with last version.
How can I send user input to my PHP script with this new version? Am I missing something here?
I'm not sure if this is the same thing, but I was running into this same problem, and I was able to get it working by deselecting the "Use Console Input" checkbox in the PHPStorm Console.
John's answer is perfect.
I want to mention that the Use Console Input is a tiny icon in sidebar of the debug console. I provide you by this image

How to Start ChromeDriver.exe without EULA with Selenium Webdriver? [duplicate]

I am learning to use Selenium (v2.20) to get ahead of some of our programmers who will soon be creating some browser tests with it. I'd like to uncover the pitfalls before they get there, and I've stumbled into one.
When I create my ChromeDriver, it always brings up a "Google Chrome EULA" and presents two buttons: "Accept and Run" and "Cancel". As I want this to be an automated test, having a user click a button is out of the question.
I looked at a list of Chromium Command Switches but did not find any that worked, nor any that mentioned EULA. The test works fine if I (at a breakpoint) click "Accept and Run" and then let the code continue.
The code, up to the line that causes the problem, is below:
using (var driverService = ChromeDriverService.CreateDefaultService(#"C:\Apps\ChromeDriver\"))
{
driverService.Start();
// This line pops up the EULA
IWebDriver driver = new ChromeDriver(#"C:\Apps\ChromeDriver\");
// rest of test...
}
Has anyone else run into this issue? If so, how did you solve it?
UPDATE 4/4/12
I just ran the same code on my computer at work and I succeed without triggering the EULA (consistent with Slanec's experience). This leads me to believe the cause is environmental. I'm looking into the differences between the two systems (both Win7 x64) to determine the cause. I'll update once I have more information.
Thanks much,
-Seth
In case you still have this problem, the error occurs because you are opening up a brand new instance of the chrome browser every time you run the test, thereby triggering the EULA. If you copy the default chrome profile into a custom location of your choice, and then add the "--user-data-dir=yourcustomlocation" flag to ChromeOptions, you can bypass the EULA and open up the existing profile instead.
ChromeOptions crOptions = new ChromeOptions();
crOptions.AddArgument(#"--user-data-dir=C:\custom location");
return new ChromDriver(crOptions);
Steps:
Copy your chromedriver.exe into Windows/System32
Now Go to your chrome folder, for me it is: C:\Users\"%USERNAME%"\AppData\Local\Google\Chrome\
There is a master_preferences file.
Open it and false EULA option.
It works for me, hope will work for you all also.

Vim: Make chrome refresh anytime I :write

I want a solution that does what's described in the vim wiki here, but that works on Chrome.
That is, I'm trying to avoid this:
Edit your HTML/CSS file.
Hit save in Vim.
CMD/CNTRL/ALT + TAB over to Firefox.
Press Ctrl-R in Firefox to refresh.
CMD/CNTRL/ALT + TAB back to Vim.
Do it again and again, wincing a little bit each time.
The vim wiki solution is for firefox, and other scripts and solutions I've found on the web are Mac only. But I'm on windows, and will often have vim open on the left half of the screen (editing html) and chrome open on the right half of the screen (displaying the file I'm editing).
A really "dumb" solution would work fine for me. That is, there wouldn't even need to be communication of the filename between vim and the browser. If I could just turn on a mode in vim, call it "auto-refresh", and now anytime I do a :w the currently active tab in chrome refreshes itself, without taking focus off the vim window. That would be perfect. Is it possible?
I don't know how to reload a give chrome page from shell, however, I agree with Chiel92 that if you need to see your changes when file changes, you can do that from browser.
See LiveReload, works with Windows & Mac (not for me then) and supports Safari & Chrome.
LiveReload will check your main page as well all css and javscript that it depends from, if any of those changes, it will reload it.
They seems to have launched a second version, however official site of version 2 it's offline and doesn't seem version 2 it's on github either. (Version 1 it's it's on github)
a solution that might work for you is in your html coding include the line in the head tag
<meta http-equiv="refresh" content="30" />
this will reload the page every 30 seconds directly taking from
w3schools.com
now when you want to deploy it just remove that line
If you are working on mac, then a bit of apple script does the job.
putting this function in your vim configuration will let you automate the process of switching between windows and refreshing you described.
function! ReloadBrowser()
silent exe "!osascript -e 'tell app \"Firefox\" to activate\<cr>
\tell app \"System events\"\<cr> keystroke \"r\" using command down\<cr>
\end tell'"
silent exe "!osascript -e 'tell app \"Iterm2\" to activate'"
endfunction
Calling that function will change system focus to Firefox, hit CMD-R to refresh the page, then change focus back to Iterm2.
Change 'Firefox' and 'Iterm2' to fit your workflow.
You can type the function into the vim command prompt like :call ReloadBrowser() or trigger the call with a mapping like this:
nnoremap <leader>rl call ReloadBrowser()<cr>
To trigger the call any time you write the file you could use an autocommand.
augroup AutoReload
au!
autocmd BufWritePost *.filetype call ReloadBrowser()
augroup END
That could get a bit annoying though so if you really want that behavior I think it would be best to make it toggle-able like this:
let s:auto_reload = v:false
function! ToggleAutoReload()
if s:auto_reload
augroup AutoReload
au!
autocmd BufWritePost *.filetype call ReloadBrowser()
augroup END
else
augroup AutoReload
au!
augroup END
endif
let s:auto_reload = !s:auto_reload
endfunction
with that in place you can either manually trigger the reload with <leader>rl or use :call ToggleAutoReload() to enable automatic reloading when you save the file, and a second :call ToggleAutoReload() will disable it when you're done.
I've tried LiveReload in the past but it wasn't a very dependable solution: it was very buggy and unstable and the installation process was too involved for too little effect.
Since then, (more than a year) I've been using a small Chrome extension that automatically reloads the webpage every x seconds. It's not "smart" at all but it works well both for local files and hosted files. I've used it countless times without any side effects whatsoever.
That Solution on the Vim Wiki is possible because of MozREPL which allows an external process to interact with Firefox. From there it's quite trivial to write an autocmd that triggers a refresh on :w or on CursorHold. But AFAIK there's no such tool for Chrome/Chromium and they don't offer an external API. LiveReload is a brilliant but failed hack and I believe that you'll have to settle with the dumb solution if you must work with Chrome.
edit
I just remembered a script that works very well on this Linux box but is a little bit limited on Mac OS X and doesn't seem to work on Windows. Essentially you register a window/tab with a part of it's name:
$ webrf setup-by-search test.html
then you simply do:
$ webrf refresh
to refresh the page.
I use WSL and two screens so for me this works. (as with every :write is a bit too much)
nmap <leader>t :silent !powershell.exe -command "Add-Type -AssemblyName System.Windows.Forms; [Windows.Forms.Cursor]::Position = \"-1290,50\"; Add-Type -MemberDefinition '[DllImport(\"user32.dll\")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W; [W.U32]::mouse_event(6,0,0,0,0); [Windows.Forms.Cursor]::Position = \"40,40\"; [W.U32]::mouse_event(6,0,0,0,0);"<CR><CR>
It basicly tells vim through a powershell command to have the mouse cursor go to:
position -1290, 50 ( which is where the refresh button of chrome is)
left mouse click
position 40,40 (which is where my vim is in full screen)
left mouse click
Step 3 and 4 is needed to refocus vim I tried to sent ALT-TAB but my vimrc disliked the % symbol.
On Windows OS you can utilize an AutoHotKey script that will automate that for a key press.
For example, the following script will bind Left Alt + r to switch to the browser window, send a Control + r(Refresh) and then get back to your previous window.
Replace Title_of_your_webpage with your browser's window title string after your page has loaded in it.
!r::
WinGet, winid, , Title_of_your_webpage
WinActivate, ahk_id %winid%
Send ^r
Send !{Tab}
Return

Playn HTML5 won't run from Eclipse

I am trying to run the Playn example projects. I followed every step in this guide to setup new Playn development environment and then this guide to run sample projects.
it seem to work fine but when I try to run the HTML5 version by right click and then going to Google-> GWT compile, nothing happens. I don't see the development mode view poping up to copy the address and paste it to web browser as the guide says. I just get the following in the console window:
Compiling module playn.showcase.Showcase
Compiling 1 permutation
Compiling permutation 0...
Compile of permutations succeeded
Linking into L:\playn-samples\showcase\html\war\showcase
Link succeeded
Compilation succeeded -- 35.187s
Beyond that nothing happens. If I right click and select run as-> web application, I get the pop out saying
Could not find any hosting pages in the project playn-showcase-html
Anybody know what am I doing wrong ?
What you got from the compilation was all good.
"Beyond that nothing happens." is okay.
When you right click on the "playn-showcase-html" project, select "Run As - (g) Web Application", you should get an output to the "Development Mode" tab as "http://127.0.0.1:8888/Showcase.html?gwt.codesvr=127.0.0.1:9997".
If not, check if you have got the following folder structures under the "playn-showcase-html" project:
playn-showcase-html
|...
|--war
|--Showcase.html
|--WEB-INF
|--web.xml
As far as I know, you can't simply compile (GWT) and run the HTML version. This is because, the HTML version requires a local web server (such as jetty/tomcat) to host the files in order for the project to be 'run'. However, a simpler way around this would be to try using ant via Eclipse.
Window > Show View > Ant
Once the window appears (probably on a sidebar), right-click and select:
Add Buildfiles...
When the list of projects appear, expand the project by clicking the small arrow to the left of the project name in the list. Then select the ant build file:
build.xml
That will add the ant build file to your list of active build files.
Expand similarly to look at the ant tasks provided by the build file.
Double click on the appropriate task; in your case:
run-html
OR
Run ant directly on the command-line to get the same results.
In the current version of PlayN a jetty server is being started automatically. Right click on the xx-html project "Run As"->"maven install". This starts the GWT compiler, and starts a jetty server (default port is 8080), then you can run the HTML5 version by typing "localhost:8080" in yout browser.
I've also wrote a more detailed description about this on my blog getting started with eclipse and PlayN , maybe this could be interesting.

Active Desktop Website to Open Programs?

I have always thought that replacing the normal windows desktop icons with an "active desktop" html page which can launch my programs, as well as open directories and files through links. This would be a really cool way to customize one's desktop.
Using the "file://" protocal was my first approach but this does not work correctly. In another SO Post I found this link but its a little over my head. Is this the correct approach?
I can't imagine that I am the first one to consider this; it seems like a good idea (no?). What do you all think?
I like the idea of active desktop for a desktop customization. What you can do is register your own schema
Add a Registry entry.
Here is the first example I worked with.
Add This to a .reg file and Import into the registry.
Change "myschema" and the location of the executable to match your own needs
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\myschema]
#="URL:My Schema Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\myschema\DefaultIcon]
#="C:\\Program Files\\MySchemaProgram\\MySchema.exe,1"
[HKEY_CLASSES_ROOT\myschema\shell]
[HKEY_CLASSES_ROOT\myschema\shell\open]
[HKEY_CLASSES_ROOT\myschema\shell\open\command]
#="\"C:\\Program Files\\MySchemaProgram\\MySchema.exe\" \"%1\""
Now if you create a hyperlink
<a href="myschema://Whatever+i+want" >Click Here</a>
Your computer will open your MySchema Application with the href in the command line.
just be mindful because Ervironment.CommandLine will be UTF Decoded therefore
//This Link
Click Here
//Will Result in This Command Line Execution.
"C:\Program Files\MySchemaProgram\MySchema.exe" myschema://Something Here
You can also use this schema in the ShellExecute commands as well as the Run Dialog.
Here is the example program I used to get started with. You should be able to get the idea pretty quick
using System;
using System.Collections.Generic;
using System.Text;
namespace Alert
{
class Program
{
static string ProcessInput(string s)
{
// TODO Verify and validate the input
// string as appropriate for your application.
return s;
}
static void Main(string[] args)
{
Console.WriteLine("Alert.exe invoked with the following parameters.\r\n");
Console.WriteLine("Raw command-line: \n\t" + Environment.CommandLine);
Console.WriteLine("\n\nArguments:\n");
foreach (string s in args)
{
Console.WriteLine("\t" + ProcessInput(s));
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
best of luck with your application. I would be eager to see it when it is complete.
EDIT: I am adding some matching strings for this as it is a cool feature and more people may want to use this technique for something they wanted to do.. These are the things i looked for it under, (unsuccessfully) Let me know if anyone thinks of any more.
custom url/uri handler
custom schema program
registry web address program
handle web link
launch app from url
I've looked into what you've asked a while back and never found anything that worked well.
Ultimately if the goal is full customization of your desktop, I think you can get a similar result by researching something like this: http://rainmeter.net/RainCMS/ and http://kaelri.deviantart.com/art/Enigma-103823591 Lifehacker has tons of links/tutorials on how to get it all running. I was able to get a desktop running that far exceeded my goals with using HTML on the desktop.
One way I got some really interesting results (though I haven't tried it for years and can't confirm right now that it still works on a modern OS and modern flash player) was to use an active desktop page running a flash swf file that contained a user interface I had created and (at the time I was using it) could launch programs from within the swf.
I ran a setup in a cyber cafe where each desktop was controlled via active desktop running a flash app. Launching and shutdown operations were handled with a mini webserver written in python. The OS was XP.
My experience was that Active Desktop is unreliable. It leaks memory terribly and regularly crashes. They might have fixed it in later versions but since hardly anyway uses it I doubt much effort has gone into fixing its obvious flakiness.