What is the equivalent of this step-by-step voice command in advanced scripting? - naturallyspeaking

I defined a step-by-step voice command:
What is the equivalent of this step-by-step voice command in advanced scripting?
I tried:
Sub Main
SendKeys "^{Pad4}"
End Sub
and
Sub Main
SendSystemKeys "{Ctrl}({NumKey4})"
End Sub
and
Sub Main
SendSystemKeys "^{NumKey4}"
End Sub
None of them works.

Sub Main
SendSystemKeys "{Ctrl+NumKey4}"
End Sub
There may be other variants that work depending on the language of your OS and Windows version. If the above does not work, try this:
Sub Main
SendSystemKeys "{Ctrl+Num4}"
End Sub
You can only use the ^ for Ctrl with SendKeys. SendSystemKeys & SendDragonKeys need the full form (Ctrl).

Related

Using Platypus to create Mac OS X applications from a perl script

Is it possible to get user input when using Platypus to build an application from a script?
I have a simple perl script. Which if I run from terminal, it asks for user input. But when I build an application file with Platypus, only the script's output is displayed.
The documentation is clear on this, no bi-directional communication; see http://www.sveinbjorn.org/files/manpages/PlatypusDocumentation.html#812
That leaves you with a few workarounds;
Use and expect script to inject your inputs;
Update your script to take arguments, which is a feature supported by platypus;
If you need to add more dynamic information, consider using a TK dialog to query for user input;
On mac you can use an osascript to call a dialog with minimum code;
OSA Script Example
#!/usr/bin/env perl
use strict;
sub osascript($) { system 'osascript', map { ('-e', $_) } split(/\n/, $_[0]); }
sub dialog {
my ($text, $default) = #_;
osascript(qq{
tell app "System Events"
text returned of (display dialog "$text" default answer "$default" buttons {"OK"} default button 1 with title "$(basename $0)")
end tell
});
}
my $result = dialog("Life, the universe and everything?", "42");

Perl run function on script exit/die

I have a question and could not find the answer i need. I have a perl script that works with several files. At the beginning of the script a given file is renamed and backed up, at the end of the script it is renamed back, so the original file is not touched at all.
But what if the script dies while running, e.g. if a file is missing or if the user exits the script via "cmd + c" on the keyboard? Is there a method to define a function that is always executed when the script dies or the user wants the script to die? I found the "END"-block from perl, but it don't think that will work in my case.
Thank you!
-Alex
The END block works for exit and die. If doesn't work for signals, though, you'll have to create a signal handler. Setting it to an empty sub would pass the control to the END block:
local $SIG{INT} = sub {};

identify a procedure and replace it with a different procedure

What I want to achieve:
###############CODE########
old_procedure(arg1, arg2);
#############CODE_END######
I have a huge code which has a old procedure in it. I want that the call to that old_procedure go to a call to a new procedure (new_procedure(arg1, arg2)) with the same arguments.
Now I know, the question seems pretty stupid but the trick is I am not allowed to change the code or the bad_function. So the only thing I can do it create a procedure externally which reads the code flow or something and then whenever it finds the bad_function, it replaces it with the new_function. They have a void type, so don't have to worry about the return values.
I am usng perl. If someone knows how to atleast start in this direction...please comment or answer. It would be nice if the new code can be done in perl or C, but other known languages are good too. C++, java.
EDIT: The code is written in shell script and perl. I cannot edit the code and I don't have location of the old_function, I mean I can find it...but its really tough. So I can use the package thing pointed out but if there is a way around it...so that I could parse the thread with that function and replace function calls. Please don't remove tags as I need suggestions from java, C++ experts also.
EDIT: #mirod
So I tried it out and your answer made a new subroutine and now there is no way of accessing the old one. I had created an variable which checks the value to decide which way to go( old_sub or new_sub)...is there a way to add the variable in the new code...which sends the control back to old_function if it is not set...
like:
use BadPackage; # sub is defined there
BEGIN
{ package BapPackage;
no warnings; # to avoid the "Subroutine bad_sub redefined" message
# check for the variable and send to old_sub if the var is not set
sub bad_sub
{ # good code
}
}
# Thanks #mirod
This is easier to do in Perl than in a lot of other languages, but that doesn't mean it's easy, and I don't know if it's what you want to hear. Here's a proof-of-concept:
Let's take some broken code:
# file name: Some/Package.pm
package Some::Package;
use base 'Exporter';
our #EXPORT = qw(forty_two nineteen);
sub forty_two { 19 }
sub nineteen { 19 }
1;
# file name: main.pl
use Some::Package;
print "forty-two plus nineteen is ", forty_two() + nineteen();
Running the program perl main.pl produces the output:
forty-two plus nineteen is 38
It is given that the files Some/Package.pm and main.pl are broken and immutable. How can we fix their behavior?
One way we can insert arbitrary code to a perl command is with the -M command-line switch. Let's make a repair module:
# file: MyRepairs.pm
CHECK {
no warnings 'redefine';
*forty_two = *Some::Package::forty_two = sub { 42 };
};
1;
Now running the program perl -MMyRepairs main.pl produces:
forty-two plus nineteen is 61
Our repair module uses a CHECK block to execute code in between the compile-time and run-time phase. We want our code to be the last code run at compile-time so it will overwrite some functions that have already been loaded. The -M command-line switch will run our code first, so the CHECK block delays execution of our repairs until all the other compile time code is run. See perlmod for more details.
This solution is fragile. It can't do much about modules loaded at run-time (with require ... or eval "use ..." (these are common) or subroutines defined in other CHECK blocks (these are rare).
If we assume the shell script that runs main.pl is also immutable (i.e., we're not allowed to change perl main.pl to perl -MMyRepairs main.pl), then we move up one level and pass the -MMyRepairs in the PERL5OPT environment variable:
PERL5OPT="-I/path/to/MyRepairs -MMyRepairs" bash the_immutable_script_that_calls_main_pl.sh
These are called automated refactoring tools and are common for other languages. For Perl though you may well be in a really bad way because parsing Perl to find all the references is going to be virtually impossible.
Where is the old procedure defined?
If it is defined in a package, you can switch to the package, after it has been used, and redefine the sub:
use BadPackage; # sub is defined there
BEGIN
{ package BapPackage;
no warnings; # to avoid the "Subroutine bad_sub redefined" message
sub bad_sub
{ # good code
}
}
If the code is in the same package but in a different file (loaded through a require), you can do the same thing without having to switch package.
if all the code is in the same file, then change it.
sed -i 's/old_procedure/new_procedure/g codefile
Is this what you mean?

Perl - Master script calling sub-scripts and return status

Here's the design I want to accomplish in Perl:
A master script calls multiple sub-scripts. The master script controls the calling of each sub-script in a particular sequence and records output from each sub-script in order to decide whether on not to call the next script.
Currently, I have a master script that calls the sub-script using a system() call, but I am having trouble having the sub-script communicate back status to the master script.
Do not want to use sub functions, would really like to keep each of the sub-script code separate.
To shed more light on the problem:
The sub script should decide what to report back to the master script. For eg: sub script sends code 1 when sub script finds a string value in the database, it sends a code 2 when the sub string doesn't find the file its looking for, and sends a code of 0 when everything goes fine.
Can't you just use exit codes for this?
my $code = system( 'perl', '-e', 'exit 2;' ) >> 8; # $code = 2
say "\$code=$code";
Exit codes can be 255 distinct values.
You can execute and capture output from system commands with backtick syntax.
# get result as scalar
$result = `ls -lA`;
# get the result as an array, each line of output is a separate array entry
#result = `ls -lA`;
Whenever you use the backtick syntax, the exit status of the command is also stored in the automatic variable $?
You can then have the master script decide if the output is good or not using whatever logic you need.
Looking at Axeman's answer you could use the IPC::System::Simple module:
#!/usr/bin/perl
use warnings;
use 5.012;
use IPC::System::Simple qw(system $EXITVAL EXIT_ANY);
system( [2], 'perl', '-e', 'exit 2' );
say "EXITVAL: $EXITVAL";

Is it possible use or require a Perl script without executing its statements?

I need to add unit testing to some old scripts, the scripts are all basically in the following form:
#!/usr/bin/perl
# Main code
foo();
bar();
# subs
sub foo {
}
sub bar {
}
If I try to 'require' this code in a unit test, the main section of the code will run, where as I want to be able to just test "foo" in isolation.
Is there any way to do this without moving foo,bar into a seperate .pm file?
Assuming you have no security concerns, wrap it in a sub { ... } and eval it:
use File::Slurp "read_file";
eval "package Script; sub {" . read_file("script") . "}";
is(Script::foo(), "foo");
(taking care that the eval isn't in scope of any lexicals that would be closed over by the script).
Another common trick for unit testing scripts is to wrap the body of their code into a 'caller' block:
#!/usr/bin/perl
use strict;
use warnings;
unless (caller) {
# startup code
}
sub foo { ... }
When run from the command line, cron, a bash script, etc., it runs normally. However, if you load it from another Perl program, the "unless (caller) {...}" code does not run. Then in your test program, declare a namespace (since the script is probably running code in package main::) and 'do' the script.
#!/usr/bin/perl
package Tests::Script; # avoid the Test:: namespace to avoid conflicts
# with testing modules
use strict;
use warnings;
do 'some_script' or die "Cannot (do 'some_script'): $!";
# write your tests
'do' is more efficient than eval and fairly clean for this.
Another trick for testing scripts is to use Expect. This is cleaner, but is also harder to use and it won't let you override anything within the script if you need to mock anything up.
Ahh, the old "how do I unit test a program" question. The simplest trick is to put this in your program before it starts doing things:
return 1 unless $0 eq __FILE__;
__FILE__ is the current source file. $0 is the name of the program being run. If they are the same, your code is being executed as a program. If they're different, it's being loaded as a library.
That's enough to let you start unit testing the subroutines inside your program.
require "some/program";
...and test...
Next step is to move all the code outside a subroutine into main, then you can do this:
main() if $0 eq __FILE__;
and now you can test main() just like any other subroutine.
Once that's done you can start contemplating moving the program's subroutines out into their own real libraries.