Skip navigation

Applescript- combine multiple IF statments and an abbreviation for home folders

690 Views 5 Replies Latest reply: Aug 6, 2012 4:44 PM by Gnarlodious RSS
svkrzn Level 1 Level 1 (20 points)
Currently Being Moderated
Aug 5, 2012 12:40 AM

i'm trying to make an applescript to make a backup of some files to dropbox. My question was if it was possible to combine multiple IF statmens in my script and if it was possible to make a shorten SET name TO path, like Macintosh HD:Users:svkrzn:Dropbox to ~:Dropbox.

Here is my script:

set destination to "Macintosh HD:Users:svkrzn:Dropbox:Backup"
set safari to "Macintosh HD:Users:svkrzn:Library:Safari:Bookmarks.plist"
set safariplist to "Macintosh HD:Users:svkrzn:Dropbox:Backup:Safari_Bookmarks.plist"
set things to "Macintosh HD:Users:svkrzn:Library:Application Support:Cultured Code:Things:Database.xml"
set thingsxml to "Macintosh HD:Users:svkrzn:Dropbox:Backup:THINGS_Database.xml"

tell application "Finder"
    try
    set S1 to duplicate file safari to folder destination with replacing
    set T1 to duplicate file things to folder destination with replacing
    if exists file safariplist then
        display dialog "It exists."
        delete file safariplist
    end if
    if exists file thingsxml then
        display dialog "It exists."
        delete file thingsxml
    end if
    set name of S1 to "Safari_Bookmarks.plist"
    set name of T1 to "THINGS_Database.xml"
on error
    display dialog "Oops, a problem occurred duplicating the file."
end try
end tell

Thank you.

MacBook Pro (15-inch Mid 2009), OS X Mountain Lion, Crucial 8GB DDR3, Samsung 830 128GB
  • Gnarlodious Level 4 Level 4 (3,220 points)

    I don't have the brainpower for your IF question, I think you mean:

     

    true and true

     

    which is the same as:

     

    0 is 0 and 1 is 1

     

     

    Hardcoded HFS paths are always a no-no, instead of "Macintosh HD" you say:

     

    tell application "Finder" to startup disk as alias

     

    which gives you a string you can append a string to.

     

    Your home folder can be invoked like:

     

    tell application "Finder" to path to home folder

     

    You assemble a string like this:

     

    tell application "Finder" to (path to home folder) & "Dropbox:Backup" as string

     

    Note that the data class is still string. You can change it to alias by saying:

     

    tell application "Finder" to ((path to home folder) & "Music") as string as alias

     

    however this had to point to Music bacause I don't have "Dropbox". An alias invokes the existence of the location and you do it in Finder. Hope that helps.

  • Gnarlodious Level 4 Level 4 (3,220 points)

    That's because you didn't really use my code, you shortcutted. When you run an Applescript, you are running it in the Applescript interpreter. For example, say this:

     

    me

     

    This means "me" or "my" is the script's handler, so the result is a reference to the script:

     

    «script»

     

    So if you say:

     

    home

     

    Applescript's interpreter doesn't know what that means and it errors. But if you tell Finder to "home":

     

    tell application "Finder" to home

     

    Finder knows home is your home folder. What application you tell to interpret code is of the utmost importance. In the example you gave, Applescript knows what "path to home folder" means and assembles your string. This is why I told Finder to do it even though Applescript does it just as well, as a reminder that it is a Finder operation. So sending your string to Finder:

     

    tell application "Finder" to exists file safariPlist

     

    will get you "false", because no such file exists (on my computer).

     

    A string is a data class of "string", you can see what class a variable is by saying:

     

    class of safariPlist

    -> text

    Class "text" isn't a filepath, it is just some text. Class "alias" tells Applescript the string is a filepath. So if you say:

     

    exists safariPlist

     

    Applescript knows it is a filepath without even invoking Finder. Pretty cool, huh? This gets useful in long scripts involving various applications. So you can minimalistically say:

     

    set safariPlist to (path to home folder) & "Library:Safari:Bookmarks.plist" as string as alias

    if exists safariPlist then

              display dialog "It exists."

              tell application "Finder" to move safariPlist to trash

    end if

    Note

    Note that in order to move a file to trash you need to invoke Finder because Applescript doesn't know what trash is. Hope that helps.

  • Gnarlodious Level 4 Level 4 (3,220 points)

    You're confusing me. The script you said returned that error couldn't possibly have, so you ran a different script from what you posted. Can you see the difference?

     

    If you received that error trying to get "Safari_Bookmarks.plist" it is most likely because no such file exists.

     

    As I explained in my last post, "alias" must always refer to a file or folder that exists. If it doesn't exist, it will throw the error you pasted. That means "alias" should only be used on a file that MUST exist, unless you wrap in a conditional block or error trap.

     

    In your last script, you ONLY set a variable to a string. Applescript doesn't care or know what the string means, it is ONLY a string.

     

    "string" and "alias" are datatypes, and every datatype has a different set of properties and behaviors. A "string" datatype is ONLY a sequence of characters, but "alias" datatype must always be a path to an actual file. Datatypes is a very elementary programming concept that apply to all programming languages.

     

    In your case, you can't even say:

     

    set safariPlist to (path to home folder) & "Library:Safari:Safari_Bookmarks" as string

    if not (exists alias safariPlist) then display dialog "No such file"

     

    See how it errors, because no such file even exists to even test for existence. You would have to say something like this:

     

    set safariPlist to (path to home folder) & "Library:Safari:Safari_Bookmarks" as string

    tell application "Finder"

              if not (exists file safariPlist) then display dialog "No such file"

    end tell

     

    In this case you need to wrap the word "file" in a Finder block, but I explained that before.

     

    If you refer to an actual file that exists, it will skip over the dialog and do nothing. On my computer, it does that because I have the file "Bookmarks.plist". Not sure what you are trying to do. If you are scripting files that may or may not exist you should at least open the folder and see what's really there:

     

    tell application "Finder" to open container of file safariPlist

     

    Keep on plugging away and you'll learn something.

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.