How to determine whether a script is run at startup or manually? - adobe-indesign

By having a script located in a subfolder startup scripts of e.g. the user script folder, one can have a script run at startup. What I would like to achieve now is having the script determine whether it is run due to that startup (and e.g. create menu items for its functions) or whether it was run manually (and then perform its main function, not caring about its startup function). How can this be achieved?

Given that the script is located within the Startup Scripts Folder:
var scriptKind = (app.activeScript.parent.name=="Startup Scripts")? "startup":"normal";
alert( scriptKind );

Related

Modify existing cmd commands

I want to modify existing cmd commands, both to change out the code to another batch file and to modify the code.
For example, when I type "calc" it opens the calculator app but I want it to open a batch script I made, and when I want to edit the "help" screen. How can I modify the code that runs when I type in existing cmd commands such as tree, help and calc?
You may not be able to replace or modify all exiting programs and/or scripts, but you can "trick" your Windows to prioritize / execute yours that are similarly named.
When you type "calc" in the command prompt, you will effectively execute the file:
C:\Windows\System32\calc.exe
You need to look at the Environmental Variable named "Path"; this is where you identify which directories should be seen by your system as "global". Any EXE, BAT or CMD you'll try to execute will look at the current directory first; and if it isn't found it will try to find it in the directories listed in "Path".
For example. Lets say you want to run your own file called help.exe; a console app that you created.
Create a folder e.g. C:\CustomBatchFiles.
Go to your computer's Advanced System Settings.
Go to Environmental Variables
You should see the following screen:
Under "System Variables", find the one called "Path", then "Edit".
Add the folder in which your custom scripts / batch files reside. Please be aware that new entries will be added to the bottom - and this list dictates the "priority" - so, once created, push the "Move Up" button until your directory is at the very top.
Click OK, and OK again to apply this new change. You might have to reboot your system; or at the very least re - open your command prompt.
From there you should be able to execute your commands no matter which directory is currently open, including "used" ones such as calc and help.
Here's a tiny program I wrote. I compiled it as help.exe, and copied it to the directory that I added as a Path (C:\CustomBatchFiles):
class Help
{
static void Main(string[] args)
{
Console.WriteLine("Start executing your program / script from here.");
}
}
It will produce the following output:

VBS Runs from Explorer but not Task Scheduler

I have a legacy VBS script that runs on a schedule via task scheduler. The script checks a folder (located in a mapped network drive) and processes the files it finds in the folder.
Recently it just stopped working. I didn't write this script, I've only inherited it. It no longer seems to find files in the folder it's looking in, even when they're there. To test we created a log file and wrote file names to it. When manually double-clicking the file and running it from explorer it writes to the log file and sees everything in the directory. If you run it from the Task Scheduler it sees no files and writes nothing to the log file. It also runs for days and never seems to exit when the scheduler runs it, even if there are no files and when I run manually it closes immediately when there are no files.
We have several other legacy scripts that are very similar in function to this one and they all work exactly as expected without issue.
This script is run as administrator (the others are as well). I set the "Start In" to it's current directory (the start in directive is not set for the other scripts and they function normally) and this did not help.
This script uses full file paths to everything (the others do as well).
It works as expected only when double clicked.

how do i execute a statement in batch /powershell just once?

I want to know how to execute a set of statements or a command in a Windows Batch file or PowerShell script to be executed just once. Even if I run the script multiple times, that particular set of code or program should just run once.
If possible give an example for both Batch files and PowerShell.
In both cases you need to make changes that (a) persist beyond running the batch file or PowerShell script and (b) are visible when you start it the next time. What those changes are would depend on how cluttered you want to leave the system.
One option would be an environment variable. You can set those from batch files with setx or from PowerShell with [Environment]::SetEnvironmentVariable. You should also set them normally to have them in the current session, just to make sure that it can't be called from that session again, too.
if defined AlreadyRun (
echo This script ran already
goto :eof
)
...
setx AlreadyRun 1
set AlreadyRun 1
or
if (Test-Path Env:\AlreadyRun) {
Write-Host This script ran already
exit
}
...
[Environment]::SetEnvironmentVariable('AlreadyRun', '1', [EnvironmentVariableTarget]::User)
'1' > Env:\AlreadyRun
This approach has its drawbacks, though. For example, it won't prevent you from running the same script twice in different processes that both existed at the time the script ran first. This is because environment variables are populated on process start-up and thus even the system-wide change only applies to new processes, not to those already running.
Another option would be a file you check for existence. This has the benefit of working even under the scenario outlined above that fails with environment variables. On the other hand, a file might be accidentally deleted easier than an environment variable.
However, since I believe your batch file or PowerShell script should do something, i.e. have a side-effect, you should probably use exactly that as your criterion for abort. That is, if the side-effect is visible somehow. E.g. if you are changing some system setting or creating a bunch of output files, etc. you can just check if the change has already been made or whether the files are already there.
A probably very safe option is to delete or rename the batch file / PowerShell script at the end of its first run. Or at least change the extension to something that won't be executed easily.

Apple script to run a shell script to get around permissions

I have created a shell .command on Mac OS X that I would like to distribute. Based on my testing, you can't execute the .command file by double clicking without changing permissions first ( making executable. ) I don't want users to use the terminal and change permissions, it's too hard for them.
The shell script creates a folder structure for a project, based on where the script is. It needs to be able to run anywhere the user puts it.
The research I have done indicates that I need to use an Apple script to run the file to prompt for the user's password to get around permissions.
Any advice on how to do this?
Distribute the file either by zip or DMG - props Thilo.

How can I run a shell script on only the next startup? Is it possible to set this up from within a script?

I am trying to make a script to run Once on next startup in OSX Lion. The script is very simple, I am just recording the time of the boot and comparing it to a previous know time. What I would really like to do though is to not have to manually add a startup item, since this will be set up on a very large number of computers.
Is there a way to add a startup item from a script to only run once?
Say your script is called script.sh... Place the script in the directory /System/Library/StartupItems/
Then, just simple add the command rm -f /System/Library/StartupItems/script.sh to the end of your script.

Resources