X-Now-Playing script :)

matija krnic <[email protected]> Sat, 21 Jun 2003 15:05:18 +0200
Newsgroups gmane.mail.40tude-dialog
Message-ID <[email protected]>
i've written a nice little script that will add an "X-Now-Playing: song"
header to your posts or at the end of them.

it works on my win2k machine with winamp 2.91, so if it doesn't work
on your computer, let me know. it should also work for e-mails, but
i haven't tried it since i use dialog for usenet only.

feedback much appreciated.



program OnBeforeSendingMessage;


// ----------------------------------------------------------------------


const
  winampWindowTitle = 'Winamp v1.x';

  WM_USER = 1024;
  WM_WA_IPC = WM_USER;
  IPC_GETVERSION = 0;

  MB_YESNO = 4; 
  IDYES = 6;
  IDNO = 7;


// ----------------------------------------------------------------------

            
function findWindow(c1, c2: PChar): Cardinal; external '[email protected] stdcall';
function getWindowTextLength(hWnd: Cardinal): Integer; external '[email protected] stdcall';
function getWindowText(hWnd: cardinal; lpString: PChar; nMaxCount: Integer): Integer; external '[email protected] stdcall';

function messageBox(hWnd: Cardinal; lpText, lpCaption: PChar; uType: longword): Integer; external '[email protected] stdcall';


// ----------------------------------------------------------------------


function findWinampHWND : cardinal;
begin
  result := findWindow (winampWindowTitle, null);
end;


// ----------------------------------------------------------------------


function winampSong (winampHWND:cardinal) : string;

var titleLen  : integer;
    tempInt   : integer;
    tempStr   : string;
    plNum     : integer;
    tempmp3   : string;

begin
  // used in case winamp is not running or the window is not found
  tempStr := 'nothing';

  // if winamp's window is found...
  if winampHWND <> 0 then
    begin
      // find the caption text of winamp's window
      titleLen := getWindowTextLength (winampHWND) + 2;
      setLength (tempStr, titleLen);
      getWindowText (winampHWND, pchar(tempStr), titleLen);
      setLength (tempStr, length(tempStr));

      // the window title is "x. song artist - song name - Winamp", where the x
      // is the playlist position, but we don't want that, so we'll remove both
      tempInt := pos (' - Winamp', tempStr);
      plNum := pos (' ', tempStr) + 1;
      tempStr := copy (tempStr, plNum, tempInt - plNum);

      // check if the string ends with '.mp3' and remove it
      tempmp3 := lowerCase (copy (tempStr, length(tempStr) - 3, 4));
      if tempmp3 = '.mp3' then
        begin
          tempStr := copy (tempStr, 1, length(tempStr) - 4);
        end;

    end; // if winampHWND <> 0

  result := TempStr;
end;


// ----------------------------------------------------------------------


function OnBeforeSendingMessage(var Message: TStringlist; Servername: string; IsEmail: boolean):boolean;    

var tempSong : string;

begin

  // don't do anything for e-mails, nobody looks at e-mail headers anyway :)
  if isEmail = false then
    begin
      // get the song name
      tempSong := winampSong (findWinampHWND);
      
      if tempSong <> 'nothing' then tempSong := '"' + tempSong + '"';

      // uncomment the line below to get a messagebox asking you if you wish
      // to include the X-Now-Playing header in your post
      // if (MessageBox(null, 'Insert X-Now-Playing: ' + tempSong + '?', 'Insert X-Header?', MB_YESNO) = IDYES) then

      // insert the x-header at the beginning of the message, dialog (or perhaps
      // the news server) will sort it out as necessary.
      message.insert (1, 'X-Now-Playing: ' + tempSong);
      
      // use this if you also wish to add the song name to the bottom of your post:
      // message.add ('');
      // message.add ('Now playing: ' + tempSong);
    end;

  result := true;
end;


// ----------------------------------------------------------------------

begin
end.


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Looking for the latest Free IT White Papers?
Visit SearchMobileComputing.com to access over 500 white papers.
Get instant access at SearchMobileComputing.com Today
http://us.click.yahoo.com/ZyjvfD/PLNGAA/uitMAA/dkFolB/TM
---------------------------------------------------------------------~->

Important Addresses:
Post message: [email protected]
Subscribe:  [email protected]
Unsubscribe:  [email protected]
List owner:  [email protected]
Group page:  http://groups.yahoo.com/group/40tude_Dialog
Dialog home page:  http://www.40tude.com/dialog/index.htm 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/