Skip navigation

How do I Create a Log File?

801 Views 13 Replies Latest reply: Apr 30, 2012 6:30 PM by John Antolino RSS
John Antolino Calculating status...
Currently Being Moderated
Apr 24, 2012 9:06 PM

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)
  • Kappy Level 10 Level 10 (221,095 points)
    Currently Being Moderated
    Apr 24, 2012 9:13 PM (in response to John Antolino)

    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.

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Apr 24, 2012 10:10 PM (in response to John Antolino)

    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

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Apr 24, 2012 10:33 PM (in response to Camelot)

    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

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Apr 25, 2012 12:16 PM (in response to John Antolino)

    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.

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Apr 25, 2012 1:29 PM (in response to John Antolino)

    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;"

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Apr 26, 2012 11:45 AM (in response to John Antolino)

    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"

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Apr 29, 2012 11:23 AM (in response to John Antolino)

    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"

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.