0
votes

I wrote my applescript app to hide the window of my wifi card. I ran into some problem checking if the window is visible or not (to avoid the command+h keypress have no effect), so I decided to use a delay 15 to make (not at all) sure the window pops up. If I run the script from the editor or by double-click on the app file, it works, but if I set it to start at the login of the user (under Settings>Account>Login Elements), it doesn't work! I tried to change the checkbox in the Save as... page of applescript editor: I tried both setting for only execute, but anything change. With the start screen option in fact it works, but it ask me a confirmation and I don't want it (I'll prefere to press cmd+h instead). Anyone can explain me why I have this issue?

tell application "System Events"
set progList to (name of every process)
set cond to false
repeat while cond is false
    if (progList contains "WirelessUtilityCardbusPCI") is true then
        delay 5
        activate application "WirelessUtilityCardbusPCI.app"
        tell application "System Events" to keystroke "h" using [command down]
        set cond to true
    else
        delay 5
        set progList to (name of every process)
    end if
end repeat
end tell

EDIT: Now it seems to work! I forgot to re- set progList to (name of every process). Now the code is correct.

2
I'm sorry, but I didn't think the code can change something. - Paciotti

2 Answers

2
votes

I see your code is working. That's great. However I'm posting this to help you learn. I see a couple small issues with your code. For example in your repeat loop you tell system events to keystroke "h". There's no need to tell system events to do that in that line because you're already in a system events tell block of code, so system events already knows to do that.

Here's how I would write your code. This doesn't require keystrokes, which is always a good thing, and is a little more efficient. It works because if the process doesn't exist then the "set theProcess to" line errors, the code then goes into the "on error" section to delay 5, then the repeat loop tries to find the process again. If the process is found then it sets its visible property which is the same as hiding it.

It also has a timeout mechanism to prevent the script from running forever. Use this if you like. Good luck.

set processName to "WirelessUtilityCardbusPCI"
set maxTime to 180 -- we only check for 3 minutes, then end

set inTime to current date
repeat
    try
        tell application "System Events"
            set theProcess to first process whose name is processName
            set visible of theProcess to false
        end tell
        exit repeat
    on error
        if (current date) - inTime is greater than maxTime then
            tell me
                activate
                display dialog "The process " & processName & " could not be found!" buttons {"OK"} default button 1 with icon 0
            end tell
            exit repeat
        end if
        delay 5
    end try
end repeat

EDIT: I have checked the above code using the TextEdit application and it works fine. To check it with your application run the following. Make sure the application is running when you run this code. If there's an error this will display it. If there's no error 2 dialogs will display showing the progress. Report what you find.

set processName to "WirelessUtilityCardbusPCI"

try
    tell application "System Events"
        set theProcess to first process whose name is processName
        display dialog "I have found the process"
        set visible of theProcess to false
        display dialog "I just performed the \"set visible\" code"
    end tell
on error theError number errorNumber
    tell me
        activate
        display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
        return
    end tell
end try
0
votes

I have used the Login Items to launch an AppleScript applet at login with success, so my first suggestion is to make sure that it's not running. Have it show a custom dialog or beep or something like that to confirm whether it's running or not.

Other than that, I'm not sure what suggestion to offer unless you'd like to post the code you're executing in the script.