Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

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

Posted on Aug 5, 2012 12:40 AM

Reply
Question marked as Best reply

Posted on Aug 5, 2012 6:23 AM

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.

5 replies
Question marked as Best reply

Aug 5, 2012 6:23 AM in response to svkrzn

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.

Aug 5, 2012 12:52 PM in response to Gnarlodious

Hello Gnarlodious,


in my code, using your suggestion, if i use

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


it works fine.


but if i use also this one

set safariplist to (path to home folder) & "Library:Safari:Safari_Bookmarks.plist" as string


it stops working going to ON ERROR function.


and the only place i use SAFARIPLIST is here:


if existsfilesafariplist then

display dialog "It exists."


deletefilesafariplist

end if


and why not using

asstring

instead of

asstringasalias

is there a difference?

Aug 5, 2012 6:11 PM in response to svkrzn

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 tohome 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 existsfilesafariPlist


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:


existssafariPlist


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 existssafariPlist 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.

Aug 6, 2012 11:13 AM in response to Gnarlodious

If you ask me if i'm dumb, yes i am... I'm very noob at apple scripting because i just started..


using your sentence

setsafariPlistto (path tohome folder) & "Library:Safari:Bookmarks.plist" asstring asalias


returns me as error:

error "File Macintosh HD:Users:svkrzn:Library:Safari:Safari_Bookmarks.plist not found." number -43 from "Macintosh HD:Users:svkrzn:Library:Safari:Safari_Bookmarks.plist"



While using

setsafariplistto "Macintosh HD:Users:svkrzn:Dropbox:Backup:Safari_Bookmarks.plist"


even if the file is not there, gives me no errors.

Aug 6, 2012 4:44 PM in response to svkrzn

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 opencontainer of filesafariPlist


Keep on plugging away and you'll learn something.

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

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.