Commands used in macro's can give unexpected results when you try and enter text values, it works from within a script file (for other commands scripts can be a problem) sometimes it's worth trying both ways. If you place the script in the file search path you can run it from a macro, or you need to include the full path (but again that can cause problems).
Try this as a script