October 28, 2009

Randomly Shuffle an Array

Filed under: Scripting — Marcus Tettmar @ 9:00 am

I was recently asked how to get a list of files from a folder, and then randomly shuffle that list. The following code retrieves the list of files to an array using GetFileList and Separate and then loops through the array shuffling it randomly:

GetFileList>c:\docs\*.*,file_list
Separate>file_list,;,files_array
If>files_array_count>0
  //randomize the array
  Let>k=0
  Repeat>k
    Let>k=k+1
    Random>{%files_array_count%-1},random_index
    Let>random_index=random_index+1
    If>random_index<>k
        Let>Temp=files_array_%k%
        Let>files_array_%k%=files_array_%random_index%
        Let>files_array_%random_index%=Temp
    Endif
  Until>k=files_array_count
Endif

Edit: As pointed out in the comments this is somewhat naive and could cause an element to be swapped back to its original position. A better method is given in the comments.

October 27, 2009

I am not Clairvoyant and I don’t have a Crystal Ball

Filed under: General — Marcus Tettmar @ 10:33 am

Honestly, I keep trying to buy a Crystal Ball and I’ve asked all our preferred suppliers, but no one seems to have one. Printers, mice, monitors, disk drives, and SATA II RAID controllers are no problem. But no one sells a Crystal Ball.

One would make my life easier. I could then answer those support responses we get. You know, the one liners like:

“Script doesn’t work”.

If we had a crystall ball I might have an inkling as to which script doesn’t work, what the user tried, how far he got, what the expected outcome was, what the actual outcome was etc?

In the absence of fictional wizardry devices there is of course the Macro Scheduler Debugger. That has all the clues. While I certainly don’t expect every user to be expert at using the debugger it is encouraging when an attempt is made to use it. This kind of support query is far more useful:

“The script isn’t working as I would expect. The output file is not being generated. I’ve stepped through with the debugger to the point where the file is supposed to be generated but in every instance the script branches to the end at line 47 because the the value of gContinue is always False and I’m not sure why.”

Great, I have something to go on. The answer is probably there in the script or the watch list but at least I know where to focus my efforts and we can do away with a game of email tennis.

Something even closer to a crystal ball is a service called BeamYourScreen which allows us to view someone else’s desktop. We can help you debug your script using this amazing technology, but I much prefer it if the other person has had a go at it first. If you get someone else to do something for you all the time you’re never going to learn how to do it yourself, so I believe a little prompting in the right direction is beneficial.

Of course, our priority support customers have access to up to 2 hours a month of BeamYourScreen support or training, and we can also offer hourly one-one BeamYourScreen consultations or training separately.

But, as yet, no Crystal Ball or clairvoyant support staff. So if you wouldn’t mind giving us a few more clues when you email support that would be much appreciated. 🙂

These links might be helpful:
Use the Debugger
How to get Support – Help us Help You

October 22, 2009

Windows 7 is Here, So Long XP!

Filed under: General, Windows 7 — Marcus Tettmar @ 9:38 am

Windows 7 Front Runner

So, unless you’ve been sleeping under a rock you’ll probably be aware that today is the day that Windows Vista gets it’s official upgrade in the form of Windows 7. We got our hands on a beta copy as soon as it became available to MSDN subscribers and back in January I was able to report that Macro Scheduler 11 is compatible with the new operating system. In fact Macro Scheduler 11 is now listed as a “Windows 7 Front Runner” application. These are applications that already support Windows 7 on launch date.

I’ve actually been running Windows 7 on my main desktop since the RTM (Release to Manufacturing) version was made available in August. It was an easy upgrade from Vista. I like it. It feels more responsive than Vista (though I have no idea if it really is) and I’ve encountered no problems. The UAC prompts that gave Vista such a bad reputation can now be manually adjusted so rather than the two options – “Off and completely insecure” or “On but potentially very annoying” – you now have a sliding scale of annoyance. Personally I never had any problems with UAC and I’ve left Windows 7 running with the default options on my main desktop.

Compared to the horribly insecure default mode of XP that allows home users to unknowingly delete system files and install trojans; UAC in Vista and Windows 7 is a godsend. The trouble with XP was that if you did try and do the sensible thing and run in a restricted user account you’d find that so many things didn’t work. It was a pain to then switch to an Administrator account in order to install some software or make a system configuration change. The problem was self perpetuating in the sense that since most users ran in the default admin mode, developers of software assumed this was the case leading to software that only worked when run under an administrator user. This in turn lead to more people running as admin and not bothering to lock down their systems.

I think pretty much every home user I know who runs XP has at some point ended up with a virus or trojan on their computer. I’ve had to come to the rescue many a time. For friends and family with children in the household I’ve always encouraged the parents to only allow their children to use a restricted account, but found that they gave up because sites and software that the children wanted to use legitimately wouldn’t work easily without first changing their account to an administrator. Not surprisingly the parent got fed up with the hassle and gave up, or forgot to set the account back to restricted, and eventually I’d be called upon to fix yet another case of weird behaviour (of the computer, not the child or parent) which turned out to be due to a virus, trojan or some other system problem brought about because as an administrator anything can happen without the user’s knowledge.

That’s why I now encourage these families to upgrade to Windows 7. When administrator privileges are required – say to install a piece of software or browser plugin – the administrator password can be entered and the job is done. They should still make sure their kids are running in ordinary accounts though, because kids will probably still click YES on every UAC box they see! As an ordinary user, if the child does something that requires administrator privileges they will need to ask their parent or whoever is the administrator to approve it and enter the administrator password if they do. Otherwise no harm can be done.

In my view UAC therefore means LESS hassle and MORE security for families and end-users than XP offered. Sure, UAC might be annoying for advanced users who make configuration changes frequently, but they could switch it off in Vista or “turn it down” in Windows 7. But for general day-to-day use I would advise against it. I’d rather know that something wants Administrator access when it tries to do something, and I can purposefully choose to start something with Admin rights when I know that I’ll need it.

When I’m not in my office I use an Asus Eee PC 1000H Netbook. It’s a nifty little lightweight machine that has only 1gb RAM and came with XP installed. The Netbook trend has been for smaller, lighter machines with a long battery life and that generally means keeping the specification simple with a basic processor and small RAM. Traditionally PCs have been going in the opposite direction with Microsoft’s operating systems getting bulkier and more demanding. So the choice for Netbooks has so far been Linux, or XP if you wanted a Microsoft OS. But we can’t keep using XP forever and Microsoft needed to make sure their new operating system ran well on Netbooks and indeed claimed that Windows 7 would support them. I was interested to find out for myself how well Windows 7 would run on my Eee PC and was keen to ditch XP anyway. I now have Windows 7 running on my Netbook and I am very pleased. It boots up quickly, appears no less responsive than before and there is no appreciable difference in battery time.

So for me Vista was an improvement and Windows 7 is the polished version. Unfortunately it’s not possible to upgrade XP to Windows 7 without doing a clean installation. I wonder if that might slow down the adoption rate, although I see that Windows 7 is the biggest pre-order item at Amazon UK, beating Harry Potter! I’m not sure what that says, but after eight long years it’s definitely time to say bye bye to Windows XP.

October 9, 2009

Introducing Macro Scheduler Lite – With 10% Discount

Filed under: Announcements, Macro Recorder — Marcus Tettmar @ 12:28 pm

Today we have released a new entry-level edition of Macro Scheduler called Macro Scheduler Lite.

Over the last 12 years, Macro Scheduler has gone from strength to strength and grown in power and flexibility to become the fullest featured, most capable automation solution on the market today. But we want to bring Macro Scheduler to more people, and not everyone needs the power of Internet protocol functions, Image Recognition, Custom Dialogs or Screen Text Capture. So for those who just want the basics we can now offer Macro Scheduler Lite.

Still a very capable automation solution, Macro Scheduler Lite contains the core easy-to-use automation features of Macro Scheduler Standard, including an intelligent macro recorder; functions to send mouse and keystrokes to other windows and applications; window and object Control functions; file system functions and the same script control structures; loops and conditionals and more.

Macro Scheduler Lite is ideal for the user just getting started with Windows Automation, who needs the essential features of an automation tool to automate straightforward but mundane and time consuming computer tasks.

Click here to compare the different editions.

We’ve also made sure there is a simple and fair upgrade path through all editions. So if your needs grow with time, you can upgrade for just the difference in price.

Macro Scheduler Lite is priced at only $49 USD. And until the end of October we’re offering a further 10% discount with coupon code: LITEISHERE. If you’re already using Macro Scheduler you may want a copy of Macro Scheduler Lite for home use, or for a colleague, or even as a Christmas present for someone in your life who could benefit from speeding up some of their computer tasks. You can use the coupon code as many times as you like, but it is only valid until the end of October 2009.

More Info | Buy Now

October 8, 2009

T-Shirt Winners for September

Filed under: Announcements — Marcus Tettmar @ 2:52 pm

MJT Net T-Shirts

After a short hiatus where we had no new names emerging in the winners list I’m pleased to say that the following two forum users will be receiving a flashy new MJT Net logo T-shirt each for points rewarded in September:

gpulawski: 20 points received
Waldo: 5 points received

Thanks to both of you for your valuable contributions on the forums and for continuing to help others out.

Each month I send T-Shirts to the people receiving the most reputation points in the previous month. So if you’re not already active in the forums, get stuck in – you could win a T-Shirt.

October 6, 2009

Macro Scheduler 11.1.15 Update

Filed under: Announcements — Marcus Tettmar @ 10:04 am

Macro Scheduler 11.1.15 is now available with the following changes:

  • Fixed: Compiled macros with -NOSTOPKEY option running slower
  • Fixed: CompareBitmaps not correctly comparing full image
  • Fixed: Text added by Command Locator cannot be undone
  • Fixed: New line not added when text added by command locator

Registered Downloads/Upgrades | Evaluation Downloads | New License Sales

October 5, 2009

More on Reading and Writing CSV Files

Filed under: Scripting — Marcus Tettmar @ 1:35 pm

More on Reading from CSV Files

In Reading from CSV Files I showed you how you could read an entire CSV file into an array of rows and columns. It used a SELECT * SQL statement to retrieve the entire table.

If you just want to read in a specific set of data you could use a more selective SQL statement. But before we can do that we need to make a slight change to the connection string. The connection string used in my last article includes the text HDR=NO which tells the database driver that the CSV file does not have a header record. If we want to be selective in what data we wish to retrieve we would need to know how to reference that data. So we would need a header record. The following example CSV file starts with a header record:

forename,age,fav_color
“sally”,28,blue
“fred”,32,green
“john”,28,yellow

We now have a way to identify each column of data. We can now change our connection string to use HDR=YES. So our code to connect to the database becomes:

Let>ConStr=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents;Extended Properties='text;HDR=YES;FMT=Delimited'
DBConnect>ConStr,dbH

We could now create a selective query. For example to select all people who are 28 years old we would use the following SQL:

SELECT * FROM csvfile.csv WHERE age=28

That would retrieve all fields. We may just want to retrieve their names:

SELECT forename FROM csvfile.csv WHERE age=28

In Macro Scheduler the code to do this is:

Let>SQL=SELECT forename FROM csvfile.csv WHERE age=28
DBQuery>dbH,SQL,fields,num_recs,num_fields

We then get an array called fields containing the results. See Reading from CSV files for example code that loops through the results.

Writing to CSV Files

As I mentioned in Reading from CSV Files, CSV files are just text files, so you could just use the WriteLn function to add a record of data to the end of the file:

WriteLn>”sally”,28,”blue”

But if you only want to add specific fields to the end of the line and want to save yourself the bother of having to correctly delimit the text you might want to treat the CSV file as a database table and use Macro Scheduler’s database functions instead.

In Reading from CSV Files we looked at using an ADO connection string and DBQuery to connect to the CSV file as if it were a database table and then using SQL to pull out the data. Well, we can do the same thing and use SQL “Update” and “Insert” queries in order to modify and insert data into the CSV file.

Here’s the code we used to connect to the CSV file as a data source:

Let>ConStr=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents;Extended Properties='text;HDR=NO;FMT=Delimited'
DBConnect>ConStr,dbH

To insert a row of data to the end of the file we can use the SQL INSERT statement as follows:

INSERT INTO csvfile.csv VALUES (“george”,19,”purple”)

In Macro Scheduler we need to use the DBExec command to execute this SQL:

Let>SQL=INSERT INTO csvfile.csv VALUES ("george",19,"purple")
DBExec>dbH,SQL,res

If you look at the connection string we used you’ll see the directive HDR=NO. This tells the database driver that the CSV file does NOT contain a header record. If we DO have a header record we should change this to HDR=YES. This also means we can specify individual column names in the INSERT statement. E.g. we may have the following CSV file:

forename,age,fav_color
“sally”,28,blue
“fred”,32,green

So now we can insert data for only two fields and because we have HDR=YES we can determine which of those fields should be set:

Let>ConStr=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents;Extended Properties='text;HDR=NO;FMT=Delimited'
DBConnect>ConStr,dbH
Let>SQL=INSERT INTO csvfile.csv (forename, fav_color) VALUES ("george","purple")
DBExec>dbh

Unfortunately it would appear that it is not possible to perform DELETE or UPDATE queries on CSV files. I’ve spent a few hours trying to find a definitive reference for JET’s text file driver but cannot find anything. All I know is that when I tried a DELETE or UPDATE query nothing happened. This is a shame as this possibility would be extremely powerful. As it is there’s not really much of a benefit over using DBExec instead of WriteLn.

If anyone knows of a definitive reference or a way to be able to perform DELETE or UPDATE queries using the Microsoft CSV driver, please let me know.

September 18, 2009

Fun with Chinese Windows

Filed under: General — Marcus Tettmar @ 11:12 am

As you may have noticed, in the last Macro Scheduler update we fixed a problem that prevented compiled macros from running on Chinese Windows. This probably affected other non-Western language versions of Windows too. It took us a while to track down this little bug even though, as is often the case, it turned it to be a simple little thing.

In order to replicate the problem quickly I installed the Chinese Simplified version of Windows XP into a Virtual Machine. That was interesting when I have no knowledge of Chinese whatsoever. Luckily (or unluckily) I’ve installed XP so many times I knew from the location of each option what each one did. Here’s our XP Windows showing the control panel:

Do what?

Do what?

It did give me a great idea for a way to stop my friends and family from mucking up their PCs. Maybe if they don’t understand what the screen says they can’t muck it up …oh wait …

Anyway, with the 490 million Chinese Internet users soon to outnumber the rest of us, I’m glad this little bug has been squashed.

September 17, 2009

Macro Scheduler 11.1.14 Update

Filed under: Announcements — Marcus Tettmar @ 2:52 pm

Macro Scheduler 11.1.14 is now available with the following changes:

  • Added: WLN_ENCODING=UNICODE setting to make WriteLn create Unicode file if file does not already exist
  • Added: WriteLn will detect encoding and output accordingly (ANSI or Unicode)
  • Added: ReadLn and ReadFile can now detect and read data from Unicode files
  • Fixed: Syntax highlighter wrongly highlighting start of block comment tokens when inside other commands (not at start of line)
  • Fixed: FTPGetDirFile issue with format of directory list and wildcard matching with some flavours of FTP server
  • Fixed: Dialog Designer: Ability to view TDialog properties with F11/Right Click restored
  • Fixed: Problem compiling on Chinese versions of Windows (and possibly other non-Western language versions)
  • Fixed: Image Capture Tool not refocusing after capture
  • Fixed: WSI_TIMEDOUT not coloured as system variable by syntax highlighter

Registered Downloads/Upgrades | Evaluation Downloads | New License Sales

September 9, 2009

Including Often Used Library Files

Filed under: Scripting — Marcus Tettmar @ 3:49 pm

If you create lots of Macro Scheduler macros you probably have pieces of code you use frequently. You might have blocks of VBScript or subroutines you often use in your macros. Copying and pasting code like that into your scripts is time consuming and also means if you ever need to change that code you’ll end up having to edit all your macros. A better way to use such “library code” is to Include it using the, yes, Include function:

Include>%SCRIPT_DIR%\MyLib.scp

This function includes the content of MyLib.scp in your macro.

Some people seem confused about how this works. It’s simpler than you might realise. It literally sucks in the code in place of the Include line and then executes it. When the script runs that Include line is actually replaced with the code in MyLib.scp. So the code in MyLib.scp actually becomes part of the overall macro.

One way to visualise how it works is to step through an Inlude line in the debugger. You’ll see the debugger pull the code into the script. Here’s a video to show you what I mean:

In the above example I’ve kept the include file small so that the entire script still fits in the editor. You’ll see the code jump into the editor and then disappear when the script ends or is stopped.

With Include you can reference commonly used functions and code. Some projects may involve lots of scripts that all use some core functions which you would put into an Include file.

Include is different to the Macro command which just “calls” another script file. With the Macro command that other script is run in isolation, almost like another process. The code isn’t shared between the two scripts. Include doesn’t just run the code, it pulls the code into the current macro so that it becomes part of it and is executed at that point. It remains part of the script until the script terminates. So the code could be subroutines or dialog blocks or VBScript blocks or anything you need the main script to reference.