13 Replies Latest reply: Apr 30, 2012 6:30 PM by John Antolino
John Antolino Level 3 Level 3 (530 points)

Hi All

 

Been up for a while trying to figure this out with no luck.

 

I created an app that will uninstall a program and all of it's files.

 

example

 

try

                    do shell script "rm -rf /Applications/TestFakeApp"

          end try

          try

                    do shell script "rm -rf /Applications/TestFakeApp2"

          end try

          try

                    do shell script "rm -rf ~/Library/Preferences/com.FakeTestApp.plist"

          end try

          try

                    do shell script "rm -rf ~/Library/Preferences/com.FakeTestApp2.plist"

          end try

          try

                    do shell script "rm -rf ~/Library/Logs/FakeTestApp*"

          end try

          try

                    do shell script "rm -rf ~/Library/Application\\ Support/FakeTestApp"

          end try

 

there are alot more paths to remove but this is just a few for example

 

I want to be able to create a log.txt file on the desktop to show what has been removed and or what could not be removed.

 

I then tried by creating a text document by using

 

do shell script "touch ~/Desktop/test.txt"

 

tell application "Finder"

          open file ((path to desktop folder as text) & "test.txt") using ((path to applications folder as text) & "TextEdit.app")

end tell

 

but I don't know what to do next.

 

1. Have it check for each file to see if it was deleted or not

2. add it into the test.txt file

3. save the file once done

 

Any help would be appriciated.



MacBook Pro, Mac OS X (10.7.3)
  • 1. Re: How do I Create a Log File?
    Kappy Level 10 Level 10 (226,765 points)

    There are different solutions. You can simply add Applescript commands to write data to a file. The Terminal command is 'echo "desired output text" >> ~/Desktop/log.txt'. The '>>' will append the desired output text to the file rather than overwriting the file.

  • 2. Re: How do I Create a Log File?
    Camelot Level 8 Level 8 (45,790 points)

    If you want to write your log to provide status (more than just shell error responses) then you're on the right track...

     

    You should start your script by opening your log file and storing a reference to it in a variable:

     

    set myLog to open for access file ((path to desktop as text) & "my.log") with write permission

    set eof myLog to 0 -- reset the file contents... eliminate this line if you want to append to an existing log

     

    Then, at any point in your script you can write some data to it:

     

    write "Something interesting" & return to myLog


    Then, at the end of your script, close the file:

     

    close access myLog

     

    Combining that with your script you could do something like:

     

    set myLog to open for access file ((path to desktop as text) & "my.log") with write permission

    set eof myLog to 0 -- reset the file contents... eliminate this line if you want to append to an existing log

     

    write "Trying to delete TestFakeApp" to myLog

    try

              do shell script "rm -rf /Applications/TestFakeApp"

      write "Deleted TestFakeApp" to myLog -- this will only execute if the above line doesn't trigger an error

    on error

              write "Error deleting TestFakeApp" to myLog

    end try

     

    --

     

    write "Trying to delete TestFakeApp2" to myLog

    try

              do shell script "rm -rf /Applications/TestFakeApp2"

      write "Deleted TestFakeApp2" to myLog -- this will only execute if the above line doesn't trigger an error

    on error

              write "Error deleting TestFakeApp2" to myLog

    end try

     

    --- ...

     

    close access myLog

  • 3. Re: How do I Create a Log File?
    twtwtw Level 5 Level 5 (4,690 points)

    you can do this compactly using a handler, like so (assuming that you have the files you want to uninstall defined in a list):

     

    set fp to open for access file ((path to desktop as text) & "my.log") with write permission

    set eof fp to 0

    repeat with thisFile in listOfFilePathsToDelete

      deleteAndLog(thisFile)

    end repeat

    close access fp

     

    on deleteAndLog(filePath)

              global fp

              if (do shell script "rm -rf " & filePath = 0) then

                        write "File " & filePath & " deleted." & return to fp

              else

                        write "Error deleting file " & filePath & return to fp

              end if

    end deleteAndLog

  • 4. Re: How do I Create a Log File?
    John Antolino Level 3 Level 3 (530 points)

    Your version of it was simpler for me to understand..

    Thank you

     

    But having 1 issue with it.

     

    I added some lines of non existing files to test if it did not delete them.

     

    the log file showed they were deleted, but the files were never there to begin with.

     

    I also added a return after each one write section so in the log file they would not be on the same line.

     

     

    try

      do shell script "sudo -v" password "" with administrator privileges

    end try

     

    do shell script "touch ~/Desktop/my.txt"

     

    delay 2

     

    set myLog to open for access file ((path to desktop as text) & "my.txt") with write permission

    set eof myLog to 0 -- reset the file contents... eliminate this line if you want to append to an existing log

     

    delay 2

     

    write "this is a test" & return to myLog

     

    delay 1

     

    write "Trying to delete Test

    " to myLog

     

     

    delay 1

    try

      do shell script "rm -rf /Test" with administrator privileges

              write "Deleted Test

              " to myLog -- this will only execute if the above line doesn't trigger an error

    on error

              write "Error deleting Test

              " to myLog

    end try

     

     

    delay 1

    write "Trying to delete test2

    " to myLog

    try

      do shell script "rm -rf /test2" with administrator privileges

              write "Deleted Test2

              " to myLog -- this will only execute if the above line doesn't trigger an error

    on error

              write "Error deleting test2

              " to myLog

    end try

     

     

    delay 1

     

    write "Trying to delete test3

    " to myLog

    try

              do shell script "rm -rf /test3*

              " with administrator privileges

      write "Deleted Test2" to myLog -- this will only execute if the above line doesn't trigger an error

    on error

              write "Error deleting test3

              " to myLog

    end try

     

     

    delay 1

     

    write "Trying to delete test4

    " to myLog

    try

      do shell script "rm -rf /test4" with administrator privileges

              write "Deleted Test4

              " to myLog -- this will only execute if the above line doesn't trigger an error

    on error

              write "Error deleting test4

              " to myLog

    end try

    close access myLog

  • 5. Re: How do I Create a Log File?
    Camelot Level 8 Level 8 (45,790 points)

    I added some lines of non existing files to test if it did not delete them.

     

    the log file showed they were deleted, but the files were never there to begin with.

     

    Ahh, yes, I should have thought of that.

     

    The 'try' will catch any error returned by the shell command.

     

    However, your shell command is using -f to force removal. In this mode rm doesn't throw an error if the file can't be found. Since rm doesn't throw an error, the 'on error' block never gets run.

     

    The solution would be to remove the -f switch from the rm commands. In this way rm can fail normally if the file can't be found, which gives AppleScript the chance to trap the error.

  • 6. Re: How do I Create a Log File?
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

    Another approach :

    You can use the verbose option (-v) in the rm command and redirect output to a log file.

    If a path does not exist, nothing will be written in the log file.

    But for a folder, application or a wilcard in the path, he wrote all the paths found, because the recursion or several elements with the wilcard.

     

    If there is no error, the line begins with a slash, otherwise it starts with "rm:".

     

    The sed command add "Deleted " at the beginning of each line that begins with a slash and it replace "rm:"  by "Error deleting" in the log file.

     

    The open command open the log file in TextEdit

     

    Here is the script as an example

    ----------------------------------

    set allPath to "/Applications/TestFakeApp /Applications/TestFakeApp2 ~/Library/Preferences/com.FakeTestApp.plist ~/Library/Preferences/com.FakeTestApp2.plist ~/Library/Logs/FakeTestApp* ~/Library/Application\\ Support/FakeTestApp"

     

    do shell script "/bin/rm -vrf  " & allPath & " &>~/Desktop/testLog.txt & " with administrator privileges

    do shell script "/usr/bin/sed -i '' 's~^/~Deleted /~;s/^rm:/Error deleting/' ~/Desktop/testLog.txt; /usr/bin/open -b com.apple.textedit ~/Desktop/testLog.txt;"

  • 7. Re: How do I Create a Log File?
    John Antolino Level 3 Level 3 (530 points)

    Camelot ------ without the -f the files do not get deleted. It needs to be in there.

  • 8. Re: How do I Create a Log File?
    John Antolino Level 3 Level 3 (530 points)

    Jacques Rioux ----- Your script is very simplifed. But let me understand correctly.

     

    it will not log a file it can't find it.

     

    does it log it if it can't modify the file to delete.

  • 9. Re: How do I Create a Log File?
    Jacques Rioux Level 4 Level 4 (1,960 points)

    John Antolino wrote:

    it will not log a file it can't find it.

    yes, it will not add a line in the log, because of the option "-f"

     

     

    John Antolino wrote:

    does it log it if it can't modify the file to delete.

    Yes, the error will be indicated after the path

    Example "rm: /Users/jack/Desktop/xyz folder: Operation not permitted"


    After treatment of  sed, the line will be changed  ---> "Error deleting /Users/jack/Desktop/xyz folder: Operation not permitted"

  • 10. Re: How do I Create a Log File?
    John Antolino Level 3 Level 3 (530 points)

    This has been very helpful.

     

    Thank you all. I got 95 % of it working.

     

    I last thing.

     

    I would like to remove the app from the dock as well, Like most uninstallers do.

    I usually see them kill the dock

     

    do shell script "killall Dock"

     

    But how do I remove the app?

    --------

     

    At the moment I added some GUI Scripting but I would perfer not to use GUI scripting, because its a bit flaky.

    Sometimes I noticed assistive device's says its on but the script does not always work stating that it is not on.

     

    do shell script "touch /private/var/db/.AccessibilityAPIEnabled" with administrator privileges

     

    try

              tell application "XYZ" to quit

      delay 0.5 -- adjust if necessary

              tell application "System Events" to tell UI element "XYZ" of list 1 of process "Dock"

     

                        perform action "AXShowMenu"

                        click menu item "Options" of menu 1

                        click menu item "Remove from Dock" of menu 1 of menu item "Options" of menu 1

              end tell

    end try

  • 11. Re: How do I Create a Log File?
    Jacques Rioux Level 4 Level 4 (1,960 points)

    John Antolino wrote:

     

    I last thing.

     

    I would like to remove the app from the dock as well, Like most uninstallers do.

    But how do I remove the app?

     

     

    Try this, this work on Leopard, i don't know on Lion

    --------------------------------------------------------------

    set appPath to "/Applications/TextEdit.app/" -- need the slash at end

     

    set PlistFile to (path to preferences folder as string) & "com.apple.dock.plist"

     

    tell application "System Events"

         set value of (property list items of property list item "persistent-apps" of contents of property list file PlistFile whose value of property list item "tile-type" is not "dashboard-tile" and value of property list item "_CFURLString" of property list item "file-data" of property list item "tile-data" = appPath) to ""

    end tell

    quit application "Dock"

  • 12. Re: How do I Create a Log File?
    John Antolino Level 3 Level 3 (530 points)

    Does not work on lion, But good suggestion.

  • 13. Re: How do I Create a Log File?
    John Antolino Level 3 Level 3 (530 points)

    thanks for the dock remover twtwtw

     

    To remove an item from the dock using applescript, you can use the following (it makes a list of all the persistent-apps entries that are not the one you're trying to remove, and rewrites that list to the plist file):

     

    tell application "System Events"

              set dockPlistFile to property list file "~/Library/Preferences/com.apple.dock.plist"

              tell dockPlistFile

                        tell property list item "persistent-apps"

                                  set appTileItems to value of (every property list item whose value ofproperty list item "tile-data"'s property list item "file-label" isnot "app name")

                                  set its value to appTileItems

                        end tell

              end tell

    end tell

    tell application "Dock" to quit