Repeat Set Desktop Picture (AppleScript)?

Discussion in 'Mac Programming' started by Darkroom, Jul 2, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i have an apple script which set's an image (Picture 1.png) as the desktop background, and than deletes the Picture 1.png file. that's fine and works. but when make another new Picture 1.png, and run the script again, the desktop picture doesn't change because it thinks that it's the same Picture 1.png that it has been already set too - but i know the script runs thru because the new Picture 1.png file will be deleted (even though the desktop picture will not update).

    i'm would like to force the Finder to update the desktop picture file because even though it's the same file name, it's a different image... i'm totally out of ideas... any thoughts?

    Code:
    set newDesktopPicture to POSIX file "/Users/[I](myname)[/I]/Desktop/Picture 1.png"
    
    tell application "Finder"
    	set desktop picture to alias newDesktopPicture
    		tell application "Finder"
    		do shell script ("rm \"" & POSIX path of newDesktopPicture & "\"")
    	end tell
    end tell
    
     
  2. lancestraz macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #2
    I'm just guessing but...
    This behavior might be happing because: when you change the desktop background, it is and loaded into the RAM and the desktop background refreshed. When you tell it to change the background to a new picture at the same path it thinks nothing has changed and does not refresh.

    A workaround would be to set the desktop to another picture (one of Leopards default ones) and then immediately change it again to the image you really want.

    You might also experience weird behavior if you set the desktop background to an image and then delete that image from the file system.
     
  3. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #3
    i don't know much about applescript, but is their a way to run the script so that picture 1 becomes picture 2 then that is a way that it can do it
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    i tried doing this already - it didn't seem to work as it just reverted to the default aura background - strange.

    yeah i was thinking something like this. but i'm not sure if i can get the desktop picture filename with apple script. it would work if i could write something like "if desktop picture is set to Picture1.png, than set desktop to Picture 2.png. else set desktop picture to Picture 1.png" or something like that.
     
  5. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #5
    can you put variables in applescript, see i don't know that much about it
     
  6. lancestraz macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #6
    This seems to work.
    Code:
    set OneTwo to {}
    
    repeat 2 times
    	tell application "Finder"
    		if exists ((path to desktop as string) & "Picture 1.png") then
    			set OneTwo to true
    			do shell script "mv ~/Desktop/'Picture 1.png' ~/Desktop/'Picture 2.png'"
    			set desktop picture to alias ((path to desktop as string) & "Picture 2.png")
    			
    		else if exists ((path to desktop as string) & "Picture 2.png") then
    			set OneTwo to false
    			do shell script "mv ~/Desktop/'Picture 2.png' ~/Desktop/'Picture 1.png'"
    			set desktop picture to alias ((path to desktop as string) & "Picture 1.png")
    		else
    			-- The file does not exist.
    			return
    		end if
    	end tell
    end repeat
    
    if OneTwo is true then
    	do shell script "rm ~/Desktop/'Picture 2'.png"
    else
    	do shell script "rm ~/Desktop/'Picture 1'.png"
    end if
    For some reason it wont work without the repeats. I'm to tired to find the bug right now... :eek:
     
  7. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    thanks for the code, but this didn't work for me. it did work for a split second, the desktop picture changed, but when the 2 images were deleted it reverted back to the default Aurora picture... and that's too bad because your code totally makes sense to me (except for the repeats)... maybe it has something to do with deleting the files after they are set? such bizarre behavior.

    [edit] even if i remove the rename and delete codes and just live with having 2 picture files it still doesn't update the desktop (code attached)... i'm curious why it worked for you? i'm using 10.5 if that would make a difference?

    Code:
    tell application "Finder"
    	if exists ((path to desktop as string) & "Picture 1.png") then
    		set desktop picture to ((path to desktop as string) & "Picture 2.png")
    		
    	else if exists ((path to desktop as string) & "Picture 2.png") then
    		set desktop picture to ((path to desktop as string) & "Picture 1.png")
    	else
    		set desktop picture to ((path to desktop as string) & "Picture 1.png")
    	end if
    end tell
    
     
  8. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #8
    i guess i'm going to have to allow the program to continuously make 16x16 desktop pictures in a folder using a for loop, or something... i've spent hours on this and i still can't get it to work. and i'm not at all strong with loops (or even know if i can use one here)... can i use a for loop here to change the file's name? like, if "NewDesktop" exists, than write "NewDesktop1" and set it as desktop, then "NewDesktop2" and set it as desktop, etc.

    here is my entire method with applescript embedded:

    Code:
    -(IBAction)changeDesktop:(id)sender
    	{
    	NSRect croppedRect = NSMakeRect(0, 0, 16, 16);
    	[self lockFocus];
    	NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:croppedRect];
    	[self unlockFocus];
    	NSData *data = [rep TIFFRepresentation];
    	NSString *filePath = @"/Users/[I](myname)[/I]/Desktop/NewDesktop.png"; // will be @"/Library/Desktop Pictures/Solid Colors"
    	[data writeToFile:filePath atomically:YES];
    	[rep release];
    	
    	NSString *myStringScript = [NSString stringWithFormat:
    							  @"\
    							  set NewDesktop to POSIX file \"%@\"\n\
    							  tell application \"Finder\"\n\
    							  set desktop picture to alias NewDesktop\n\
    							  end tell", filePath];
    
    	NSDictionary* errorDict;
    	NSAppleEventDescriptor* returnDescriptor = NULL;
    	NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:myStringScript];
    	returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
    	[scriptObject release];
    	}
    
     

Share This Page