Friday, December 31, 2010

PHP Script to send PDF file as attachment (requires FPDF object)

function mail_pdf_as_attachment($pdfobj,$faxnumber) {
$pdfdoc = $pdfobj->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
$to = "destinationaddress@destination.com";
$from = "sourceaddress@source.com";
$subject = $faxnumber;
$message = "See attached pdf file";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "medfax6.pdf";
//headers
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
mail($to, $subject, "", $headers);
}

Dual Formatting of External Hard Drives (PC and MAC)

You can do that?? It turns out, yes.

Plug the drive into a USB port on the mac. Go into Disk Utility (You can find the utility fastest by clicking on the "magnifying glass" shaped icon on the far upper right corner of the screen, which is called Spotlight, and typing Disk Utility.

Then partition the drive, selecting the option to create 2 partitions. This will format the external drive. Select mac formatting for one and MS-DOS FAT for the other. The trick is go go into "Options" after you enter the Partitions tabs and under options select the bottom of the three options, which allows you to create a root directory that will allow the MS-DOS partition. Otherwise, it isn't even an option.

After being done formatting, you will see both partitions on the mac with their assigned names, and if you plug into a PC you will only see the PC partition with its name and letter, like E: as assigned by the PC.

Hosting Content on the World Wide Web on a Laptop

I have a Macbook Pro (MBP) running Snow Leopard. It is connected to the internet through a wireless router. I am running MAMP on it and using it as a server, and I access the server from the iPhone, or the iPad, from WITHIN the LAN (behind the router) via the Wi-Fi connection to that router. Wouldn't it be nice to be able access that same server remotely, from OUTSIDE the LAN, via, say, a 3G connection?

Three steps to doing this:
1. Find out what the IP address is (whatismyip.com)
2. Tell the ROUTER to assign a static IP address to the MBP within the LAN
login to router as admin ->LAN setup->Use Router as DHCP Server->Address Reservation
In my case, I just selected the address it had randomly assigned to make it permanent (192.168.1.18)
You need to know your computer's name and/or mac address to do this, so that you select the correct computer.
3. Then you need to go to port forwarding (still in admin in the ROUTER settings), and assign start port 8888 to forward to end port 8888 on 192.168.1.18.

Finally, step outside the LAN and browse to:
ipaddress:8888/homefolder/index.php
and you are there.

CAUTION: if you do this and leave your server on, your precious MBP is on the world wide web, and can be subject to cyber attack. It helps that the port is not 80, or 8080, which are far more common, but still, take precautions.

Saturday, November 6, 2010

AHK (AutoHotKey) Script to control Dialog Box

iWeb_Init()
SetTitleMatchMode,2
ControlGet,hWnd,Hwnd,,Internet Explorer_Server1,ahk_class Internet Explorer_TridentDlgFrame
pDoc:=IE_GetDocument(hWnd)
pWin := iWeb_DomWin(pDoc)
COM_Invoke(pWin,"Document.all.item['btnID'].click")
COM_Release(pwin)
WinWaitClose, Web Page Dialog
SoundPlay, *64
Com_Release(pDoc)
iWeb_Term()
return

Note: the three indented lines can be replaced with
iWeb_clickDomObj(pDoc,"btnCancel")
accomplishes the same thing but I left the three lines in as a demonstration of how to inject javascript into an AHK script.(There isn't an iWeb function for everything)

Note: You have to #include COM.ahk and iWeb.ahk for this to work, and I commented out the MsgBox line in the COM_Error() function within COM.ahk in order to stop useless error messages from popping up. These libraries were not designed for dialog boxes, but they work, and the error messages just get in the way. Normally calling COM_Error(0) does the same thing except the iWeb functions keep calling COM_Error(1)

Note: ['btnID'] can be replaced with ['387'] or whatever the element index number is.
Notice the WinWaitClose to wait for the Dialog to close, and the Sound alert command. You can also use WinWait to just wait for the Web Browser Object to get focus.

Monday, November 1, 2010

Access to Snow Leopard MAMP server and AHK server from a Windows Domain

For months I have been using a MacBook Pro with OSX 10.4 as a server at work to house my MAMP scripts and AHK scripts. In a previous post I described how to network a MacBook Pro with OS X 10.4 to a windows domain. IT TURNS OUT THOSE INSTRUCTIONS DO NOT WORK FOR SNOW LEOPARD (OS X 10.6). I brought in a back up MacBook Pro and much internet research did not generate a solution for weeks. Until Now.

The secret: On the Mac go to System Preferences>Network>Advanced>WINS>NETBIOS Name. This is where the name of the computer resides. I had been putting the name of the computer into my windows domain machines as specified in System Preferences>Sharing>File Sharing for weeks in a futile effort to connect. It was the wrong name. Thus, for MAMP server the URL is http://netbiosname:8888/MAMP and for the AHK scripts the server is \\netbiosname\macusername. It's that simple. I swear, nobody in the universe knows this little factoid.

Whether it made a difference or not, some other system adjustments were made according to "internet advice". They may be necessary too, I am not sure. All I know is I did them before I found that last little secret and the whole system works very nicely. Those other things included setting the workgroup to to MACPC (not sure why that would matter, all of the windows machines are in a domain, not a workgroup), of course making sure the mac is connected to the correct wireless router with the same network as the other machines (again, not sure if it matters, since most of the machines are connected by ethernet to the network), and I used my "Launcher" on my mac to run "Directory Utility" and put a checkmark in the "Activate Directory" box. For what it's worth. And I went into aforementioned sharing and did everything in my previous post, plus activated privileges for guests, added myself as a user, activated ftp, etc. (Note- the "Windows Sharing" pane present in 10.4 doesn't exist in 10.6) Have fun with this. Networking is a real bear.

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);