I use a program to deal with the contents in sd card on startup. But the problem is that the program always starts before I can see the sd card in the finder. As a result, the program can't find the contents and can't run correctly. I think whether I can delay the startup time of a specified program in login items preference
.
2 Answers
Make a simple bash script, that starts on startup instead:
#!/bin/bash
#Wait for however long you want:
sleep <num of seconds>
#Use the following if the program is a .app:
open -a /path/to/app/myApp.app
#Use the following if the program is an executable (not a .app):
/path/to/exec/theExec
Now, save the file to your documents folder as startupScript.bash.
Note: If you use textedit to create the file, hold down shift + command + T before saving it.
Open up "Terminal". Type:
chmod +x ~/Documents/startupScript.bash
Then open up System Preferences, and add the script to the login items tab.
In addition to the nice answer contributed by Flare Cat, there is a way to do this using Automator. Because Automator is fun, and why not?
Here are the steps for delaying (pausing) a process, launching an app, and then hiding that specific app using AppleScript:
- In Automator create a new Application.
- Select the Utilities category from the sidebar.
- In the search field, type “pa” and drag a Pause action into the workflow area.
- Type “appl”, and drag over a Launch Application action. Specify the app using the drop-down.
- Drag over a Run AppleScript action.
- Inside the AppleScript text field, add the following script and specify your
app name
:
tell application "System Events" to set visible of application process "app name" to false end
- Tip: If you don't know your
app name
, you can probably find it using Activity Monitor while the app is running.
- Save your new launching app somewhere (such as your Applications folder).
- Under System Preferences → Users & Groups → Login Items, add your newly created .app as a Login Item. If the target app (linked directly, without delay) was already in the list, remember to remove that old one.
Below is an example screenshot of my configuration. In my case I have some “malware” that I am required to run for work. Due to the lag that its bloat exerts on my system, I decided to delay this app's launch by several minutes and first allow macOS the chance to start up unhindered.
Update: Although I didn't think of it until after posting this answer, it might be a good idea to also add a 1-second pause after the app is launched and before the script is executed. This should ensure the app is ready to receive the hide command. There was at least one instance when the script fired too early and my app didn't get hidden.