The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   managing cookies with applescript (http://hintsforums.macworld.com/showthread.php?t=20150)

ali baba 01-29-2004 09:57 AM

managing cookies with applescript
 
hello there,

i made a poorman's cookiecutter with applescript. what it does is replace the cookies.plist inside the prefrences folder with a backup i keep in the documents folder. of course the backup contains the cookies i wanna keep.

the script:

tell application "Finder"
duplicate "bigwig:Users:alibaba:Documents:Cookies.plist" to "bigwig:Users:alibaba:Library:Cookies" with replacing
end tell

the problem:

i have set this applescript as login item and that works fine but sometimes i don't reboot for a long time and i then i want to run it intermittently.. the problem is that sometimes it won't run and the script will give me an error message saying "operation could not be completed ". it doesn't matter whether safari is running or not. restarting the finder always solves this issue but i don't like that extra step.. i'd like to understand why it won't run sometimes and how to fix it.

.. also if someone has a alternative way for this poorman's solution i'd like hear that too of course.

tia, ali baba

ali baba 01-29-2004 10:02 AM

hey now
 
that smilee after alibaba was not intended! it's a colon plus captial d that turned into smilee. i am not the kind of guy to do such a thing. please forgive me if someone is insulted!!

mervTormel 01-29-2004 12:52 PM

you can [x] disable smilies in your posts. examine options frame on a 'post' page.

robJ 01-29-2004 02:12 PM

Re: managing cookies with applescript
 
Quote:

Originally posted by ali baba the script:

tell application "Finder"
duplicate "bigwig:Users:alibaba:Documents:Cookies.plist" to "bigwig:Users:alibaba:Library:Cookies" with replacing
end tell
I don't see how the script could work at all and I have no explanation for why it would work only sometimes. Does this work any better or worse?
Code:

tell application "Finder"
        duplicate file "bigwig:Users:alibaba:Documents:Cookies.plist" to ¬
                folder "bigwig:Users:alibaba:Library:Cookies:" with replacing
end tell

-- Rob

ali baba 01-29-2004 02:41 PM

Re: Re: managing cookies with applescript
 
hi rob,

alas same results for both scripts, they both work or they both don't at given a situation

and if i use:

tell application "Finder"
duplicate "bigwig:Users:alibabaocuments:Cookies.plist"
end tell

then i get the same error in case it doesn't work. so i get the impression that the "with replace" is not the issue (yet). duplicating itself causes an error to begin with.. as if the finder thinks that file is already in use

robJ 01-29-2004 02:47 PM

I wonder if it would help to delete the file first.

Code:

tell application "Finder"
        delete file "bigwig:Users:alibaba:Library:Cookies:Cookies.plist" -- moves it to the trash
        duplicate file "bigwig:Users:alibaba:Documents:Cookies.plist" to ¬
                folder "bigwig:Users:alibaba:Library:Cookies:"
end tell

Regarding the file in use, I wonder if a command from the shell would have the same issue.

-- Rob

ali baba 01-29-2004 03:03 PM

Quote:

I wonder if it would help to delete the file first.

Code:

tell application "Finder"
        delete file "bigwig:Users:alibaba:Library:Cookies:Cookies.plist" -- moves it to the trash
        duplicate file "bigwig:Users:alibaba:Documents:Cookies.plist" to ¬
                folder "bigwig:Users:alibaba:Library:Cookies:"
end tell


alas tried that before too.. again the replacing bit is not where it gets stuck (yet)

[/QUOTE]
Regarding the file in use, I wonder if a command from the shell would have the same issue.
[/QUOTE]

woooooooo shell, please tell me more, i'm still a rookie with that

ali baba 01-29-2004 03:13 PM

Quote:

Originally posted by mervTormel
you can [x] disable smilies in your posts. examine options frame on a 'post' page.
thanks merv, trying to work it..

robJ 01-29-2004 03:39 PM

Quote:

Originally posted by ali baba woooooooo shell, please tell me more, i'm still a rookie with that
I don't have enough shell experience to offer good advice so I'll leave that to others. If you find a shell command to delete the file, you can, if you wish, still use it in AppleScript via the "do shell script" command.

-- Rob

ali baba 01-29-2004 04:49 PM

Quote:

I don't have enough shell experience to offer good advice so I'll leave that to others.
thanks for helping so far

Quote:

If you find a shell command to delete the file, you can, if you wish, still use it in AppleScript via the "do shell script" command.
-- Rob
i remember inputting some lines in cronnix that's about all i know.. not sure what you mean above..

i'll sit here and wait until a kindred soul comes along who knows shell then

ali baba 01-29-2004 04:52 PM

man i don't even know what kindred is, i thought it meant friendly

robJ 01-29-2004 05:22 PM

According to Spell Catcher:

Quote:

1. kin·dred
• (noun)
– related individuals
– kin
• (adjective)
– of a like nature
Regarding running a shell command from an AppleScript script, it might look something like this (grabbed from someone else's script):

Code:

do shell script "rm -f " & (quoted form of (POSIX path of "bigwig:Users:alibaba:Library:Cookies:Cookies.plist"))
I'm just not absolutely sure that this is the correct command to delete the file without doing harm to other stuff. It appears so, according to the manual for rm, but I would rather one of the shell experts weigh in here.

If you decide to test it, make sure you do it on a copy because this does not simply move the file to the trash - it deletes it.

-- Rob (who is more comfortable with pure AppleScript) ;)

ali baba 01-29-2004 06:00 PM

Quote:

According to Spell Catcher:
– of a like nature
well that's definately not what i meant, that wouldn't solve much :-)

Quote:

Regarding running a shell command from an AppleScript script, it might look something like this (grabbed from someone else's script):

Code:

do shell script "rm -f " & (quoted form of (POSIX path of "bigwig:Users:alibaba:Library:Cookies:Cookies.plist"))
I'm just not absolutely sure that this is the correct command to delete the file without doing harm to other stuff. It appears so, according to the manual for rm, but I would rather one of the shell experts weigh in here.
yes rob adventure lurks here.. let's let the partywhips proofread it first.. i'm not in a hurry.. and thanks again, later, ali

acme.mail.order 01-30-2004 04:32 AM

Instead of beating Applescript over the head to do a shell job, just use the shell directly.

Save the following somewhere meaningful, like ~/Library/Scripts/cookiemanager.sh

Code:

#!/bin/sh
cp /bigwig/Users/alibaba/Documents/Cookies.plist /bigwig/Users/alibaba/Library/Cookies/Cookies.plist

`chmod +x cookiemanager.sh` to make it run

use Cronnix
to create a line like the following:
Code:

15  */6 * * * /bigwig/Users/alibaba/Library/Scripts/cookiemanager.sh
Runs every 6 hours at 15 minutes past, completely in the background.

ali baba 01-30-2004 09:26 AM

hi acme.mail.order, thanks for responding, i understood the cronnix part but have some questions

Quote:

Save the following somewhere meaningful, like ~/Library/Scripts/cookiemanager.sh

Code:

#!/bin/sh
cp /bigwig/Users/alibaba/Documents/Cookies.plist /bigwig/Users/alibaba/Library/Cookies/Cookies.plist


do i put this in a textfile and save it with that .sh suffix?
i really have no idea

Quote:

`chmod +x cookiemanager.sh` to make it run
is that a typ- in-terninal thing?

ali baba 01-30-2004 09:30 AM

Quote:

Originally posted by ali baba
well that's definately not what i meant, that wouldn't solve much :-)
oh rob, i was thinking of myself only when i wrote this, akin to me wouldn't solve much. it struck me above may sound as if i am shouting you are useless. i am sure you are not. there is no end to my clumsiness :cool:

robJ 01-30-2004 11:04 AM

ali baba, no offense taken. :)

-- Rob

acme.mail.order 01-31-2004 05:51 AM

Shell scripts must be plain, plain text. Textedit can do it, but its better if you use the Terminal editors.

Open Terminal (Applictions -> Utilities) and type the following:
Code:

cd Library/Scripts
# get an error message? type `cd Library` instead

pico cookiemanager.sh
#!/bin/sh
cp /bigwig/Users/alibaba/Documents/Cookies.plist /bigwig/Users/alibaba/Library/Cookies/Cookies.plist

<control-o> #control, not command key! BIG difference!
<enter> 
<control-X>
chmod +x cookiemanager.sh

Now, quit the Terminal and do your stuff in Cronnix. Your path MUST be exactly correct, and start from the computer root.

Hmmmmmmm - I think you should remove /bigwig from the above script, unless you have done funny things to your system paths. bigwig is the name of your one and only drive? Yes? Then remove it from the shell and Cronnix scripts.

And do a Google search for an introduction to unix.

ali baba 01-31-2004 09:43 AM

thanks a bunch it's working

you were right about the paths, it only works when i leave off my volume-name. that goes for the path inside the cookiemanager.sh as well as in the crontab itself. but i do have other drives.. can this become an issue?

is there a way to invoke/run the script quickly from the terminal so i can run it intermittently? typing the cp command with the paths is so hassle-some.

if you care could you explain what "chmod +x cookiemanager.sh" does. i did notice it doesn't work without it but i thought chmod was a permissions command and when i look at the permissions via the get-info window it's setting are the same before and after that command.

Quote:

And do a Google search for an introduction to unix.
yes i have on more than several occassions but you know how it goes.. if you don't do it often the unix-language doesn't stick well and many times i get stuck because there is this tidbit i don't grasp or i lose interest because it gets too abstract. i 'm glad you guys and girls can help out here.

acme.mail.order 01-31-2004 10:33 AM

chmod is permissions, but permissions include eXecute - the file is program code rather than a recepie for borscht. Unix only runs files with the 'x' bit set, the Mac finder doesn't care (at least not at this level)

to run the program, first try the Apple way - double click on it. Doesn't work on 10.2 unless you change the .sh to .command
Unix and the script don't care in the slightest what the filename is.

to run the script from the command line, `cd` to the directory and then type ./scriptname
or type the path to the script and it will run.

You will find your other drives in the Terminal in the /Volumes folder, but don't mess around with the /Volumes folder content until you understand unix mount points. Do not attempt to backup, restore, delete, copy, fold, spindle or otherwise mutilate this folder.

as far as your scripts are concerned, replace `bigwig` with `/` and you will be on the right track. Other drives can be reached by `/Volumes/Drive\ name\ with\ spaces/folder1/folder\ 2/file`

ali baba 01-31-2004 11:10 AM

Quote:

to run the program, first try the Apple way - double click on it. Doesn't work on 10.2 unless you change the .sh to .command
Unix and the script don't care in the slightest what the filename is.

to run the script from the command line, `cd` to the directory and then type ./scriptname
or type the path to the script and it will run.
strangely i didn't get any of the above to work.. so i have made an do shell applescript :-) almost back where i started

Quote:

You will find your other drives in the Terminal in the /Volumes folder, but don't mess around with the /Volumes folder content until you understand unix mount points. Do not attempt to backup, restore, delete, copy, fold, spindle or otherwise mutilate this folder.
don't worry, changing the drive of my name was a userlevel thing. doubleclick name of the drive in finder and change it.

Quote:

as far as your scripts are concerned, replace `bigwig` with `/` and you will be on the right track. Other drives can be reached by `/Volumes/Drive\ name\ with\ spaces/folder1/folder\ 2/file`
do you mean i use:

"cp //Users/me/Documents/Cookies.plist //Users/me/Library/Cookies/Cookies.plist"

double slash before "Users"?

acme.mail.order 01-31-2004 08:23 PM

no, single slash.

cp /Users/me/Documents/Cookies.plist

as for running the script, did you set permissions properly?

to check, cd to the same directory and type `ls -l` (lower case 'L', not number one)

example output:
Code:

-rwxr-xr-x  1 admin  wheel  78  7 Jan 00:17 sendip
-rw-r--r--  1 admin  wheel  82 29 Jan 09:17 stage2

the `x` in the array at the left is the execute permission. In this example, sendip will run, but stage2 will not. If I change the name of sendip to sendip.command I can double-click on it in the finder, but only if the execute bit is set.

Copy the results of `ls -l` of the folder containing your script and we'll see what's wrong.

ali baba 02-01-2004 05:13 AM

hi acme.mail.order,

the changing .sh into .command works fine!
i didn't get that yesterday due to excessive juggling around with files and then mixing up. the permissions of the script were set as in your serendip example.

when i asked you for a terminal way to trigger the script i had forgotten about rob's hint to use a do shell applescript. for the smoothest user experience i'll go with that for now but it's fun to know the geeky way too.

i feel like coyote who gets his acme mail order, anxiously upwraps the package, folds out the blueprint, succeeds in building it.. and then.. on to his next disaster, but that's another story.

thanks for teaching me some extra unix :) later, ali baba

acme.mail.order 02-01-2004 05:27 AM

Quote:

Originally posted by ali baba
i feel like coyote who gets his acme mail order, anxiously upwraps the package, folds out the blueprint, succeeds in building it.. and then.. on to his next disaster, but that's another story.
Guess how I came up with the user name. That, and I used to live in Acme when I repaired parachutes for a living. Just couldn't pass that address up.

ali baba 02-01-2004 07:15 AM

:)

repairing parachutes, shellscripts, cron, coyote, acme mail orders, pico, cookies, kindred smilees on the loose.. it's been a fun thread! later, ali baba


All times are GMT -5. The time now is 06:08 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.