Sunday, September 5, 2010

Examine Mouse Positions in AHK

A truly useful script to have running while writing Mousey Scripts.
I have mine set to z

CoordMode, Mouse, Screen
MouseGetPos, X, Y
MsgBox %X%, %Y%
return

AHK, Mouse Clicks, and Remote Desktop Connection (RDC)

I was having fits with mouse clicks in AutoHotKey scripts not clicking on the right spot on the screen. I originally "solved" this by including the all - important line

CoordMode, Mouse, Screen

at the beginning of each AHK script, which tells the script to override the default behavior (coordinates relative to active window) and uses coordinates relative to the actual monitor dimensions. This eliminates errors due to the wrong window (like WebDiagLog Boxes) stealing the focus. But here is another factor to consider:

When you RDC to a computer and run AHK scripts on the server computer, the coordinates of mouse clicks will vary based on the screen resolution settings on the client computer. The converse is true as well: if you author a script remotely on a client computer using RDC, when to go to run the script locally on the server without RDC the coordinates revert to the local monitor.

Here is how I deal with this:

CoordMode, Mouse, Screen
WinGetPos,,,currentw,curretnh,Program Manager
authorw := 1280
authorh := 800
wfactor := currentw/authorw
hfactor := currenth/authorh
x := 36
y := 170
xfinal := x*wfactor
yfinal := y*hfactor
Click %xfinal%, %yfinal%

Now any Client computer can author a script on a server, and it will run locally on the server and on any other client, no matter what the screen resolution is.

Addendum:
You can also examine the screen resolution using this javascript bookmarklet:
alert(screen.width);
alert(screen.height);

Sunday, August 15, 2010

Windows and Mac on same Network

I have a MAMP Server on my LAN. Browsing to it from within the network, or FTPing to from within the network can be done using the IP address, but that could be dynamic and change (particularly if I take the server home and use it on a different LAN). I browse or FTP to the server from various devices (Windows Vista, Windows XP, iPod, iPhone, iPad. I was going crazy because the apparent computer name was unrecognized in a pattern that I didn't understand. UNTIL NOW !!

Here is the great secret: MAC PROVIDES 2 DIFFERENT COMPUTER NAMES FOR ONE SERVER: ONE FOR WINDOWS DEVICE CLIENTS AND ONE FOR APPLE DEVICE CLIENTS. You find this in Mac System Preferences --> Sharing --> Windows Sharing. At the top it says "Other computers on your local subnet can access this computer at lowername.local". That's for Apple clients. For Windows clients, use the computer name in text field above that message, which can be totally different, which I will call uppername. Even if you force it to be the same, it is still without the ".local".

For FTP, if you highlight Windows Sharing, it says "Windows users can access your computer at \\ipaddress\firstnamelastname". You can substitute the computername for the ipaddress, and now when you browse to the device on the network it prompts you for username and password. Your username is firstnamelastname, which corresponds to the name of your user folder on your mac inside the "Users" folder, and your password is your mac password. Highlighting the various other checkbox sharing options gives you all kinds of useful information for networking in addition to this.

So in summary:
Apple devices browse to: lowername.local:8888
Windows devices browse to: http://uppername:8888

Apple devices ftp to: Surf to uppername within Finder
(Finder --> [Command]-K --> afp://uppername._afpovertcp._tcp.local)
Window devices ftp to: \\uppername\firstnamelastname from either networks, browser, or Windows -->Run.

And of course, you can browse to the MAMP server from the server itself within Safari:
http://localhost:8888

Addendum: MAMP installation puts the root directory by default inside the Applications Folder, in subdirectory MAMP/htdocs. Everything inside Applications folder is inaccessible over FTP. To change the root, go into that little MAMP window and there is a big "preferences" button on the lower right (below Stop Servers, Open Start Page). Go to Apache tab. Change document root to /Users/firstnamelastname/htdocs and of course move the entire htdocs folder to that location. Also note that the default port is 8888 with MAMP. That can be changed to a different port if necessary, but you have to include the port after a colon in MAMP. (Be aware that WAMP uses a different port by default, I think 8080, and you can get away with omitting the colon and port number in WAMP).

Tuesday, July 27, 2010

Debugbar, Autohotkey, Javascript

Great discoveries this past week. First of all, Debugbar is a great plugin for Internet Explorer for examining the DOM of websites, especially those that have complex dynamically created DOM with javascript. Also you can run javascript in the Debugbar console on the website without resorting to "navigating" out of the URL (you don't have to type the script into the URL input field, or navigate out of the native URL using links or favorites), which makes running these scripts possible when you have Web Controls/Agents on your source URL.

You can access the data within a frame in the DOM using the following javascript syntax:

str = top.myframename.document.GetElementById('myelementid').innerHTML;

and then use javascript REGULAR EXPRESSIONS (Regex) functions to parse the data into usable formats

Autohotkey is capable of launching javascript, but it is trickey. You have to either use the URL input field or Debugbar Javascript Console to do it, neither of which is elegant, or you have to use the COM library which is documented on the Autohotkey forum, again, which will require a learning curve. A better solution is to use Autohotkey to reliably click the correct objects in the browser, and the two tricks I have learned are:

CoordMode, Mouse, Screen

That line allows you to accurately base Mouse Movements and Click Coordinates to be relative to the screen instead of to the window, which helps when the script is popping up windows all over the place. The other trick is

Sleep, 100

Between each Click, it's good to pause the program for 1/10th of a second to let the computer catch up with itself. Prevents errors.

Monday, July 5, 2010

PHP Script accept query string data and place onto PDF form

require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile('../pdfdoc\MedSolMRICTheadneck.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 200);

$pdf->SetFont('Arial','B',10);
$pdf->SetTextColor(0,0,255);
$pdf->SetXY(56, 50);
$pdf->Write(0, $_GET['lastname'].'---'.$_GET['firstname'].'---'.$_GET['dob']);

$pdf->Output();
?>

Notes:
-The $_GET[] array names are in quotes to make WAMP happy and match the query string names the the javascript bookmarklet that sent the query
-The main work in writing this is finding the XY coordinates in the third to last line
-Copy and paste the 4 line block for each data entry point on the form, each with its own XY coordinates
-Note the color 0,0,255 (blue). Other useful colors: 0,0,0 - black and 255,0,0 - red.
-Note the 'B' parameter in font for bold: helpful when filling out forms.
-Note the source file path with the backslashes again to make WAMP happy.

So, big picture: You've invoked a bookmarklet to navigate from a website with data to a PHP script on WAMP which pulls up a PDF document in your localhost directory tree and inserts the data on the form. Now you run your AutoHotKey script to print or fax it.

AutoHotKey Script for Emailing a PDF File

:*:][::

Send {Click 1228,100}
Send {Click 1145,254}
SetKeyDelay,700,100
Send !a
SetKeyDelay,50,100
Send doc@scanr.com
Send {Tab}{Tab}{tab}^a
SendRaw +18005551212
Send !s

return

The keystrokes ][ trigger two mouseclicks to pull "send page" from EI7, followed by keystrokes that send the attachment to an online faxing service.

(Note: the asterisk near the beginning causes the script to execute immediately with the ][ trigger, without a following spacebar. Sometimes, you need to precede the execution with a spacebar keystroke to "clear" previous keystrokes.)

Javascript to fetch and submit data

This script will fetch data from the input fields of a web site and submit the data as a query string to a URL of your choosing:

javascript:
var lastname_elem = document.getElementById
('ctl00_ContentPlaceHolder1_txtLName');
var lastname = lastname_elem.value;
alert(lastname);

var firstname_elem = document.getElementById
('ctl00_ContentPlaceHolder1_txtFName');
var firstname = firstname_elem.value;
alert(firstname);

var dob_elem = document.getElementById
('ctl00_ContentPlaceHolder1_txtDOB');
var dob = dob_elem.value;
alert(dob);

open('http://example.com?lastname='+lastname+
'&firstname='+firstname+
'&dob='+dob,
'_self',
'resizable,location,menubar,toolbar,scrollbars,status');

You just need to find out the element id for the input fields. This example pops the data from the input fields into alert text boxes before submitting the get request to the next web site, for debugging purposes.