Applescript isn't moving my folder

Discussion in 'Mac Programming' started by registudio, Sep 18, 2014.

  1. registudio macrumors member

    registudio

    Joined:
    Aug 8, 2013
    Location:
    Montreal, QC
    #1
    Hi Guys,

    First things first I'm a beginner and I'm trying my first script to count periodically (lets say every 5min) a number of files in a selected folder to move them after to another destination when the appropriate known number is reached.

    However, for now I'm stuck to the moving files process for some reason.
    I'm sure I have missed something somewhere but I can't figure it at all ;o(

    Next will be to set the cycling checking event, well any clue would be appreciated.

    Here is my start:

    Code:
    display dialog "How many render frames will you have?" default answer ""
    set numberResponse to text returned of result
    set renderFolder to choose folder with prompt "Please select directory."
    tell application "System Events"
    	set item_list to POSIX path of every disk item of renderFolder
    end tell
    tell application "Finder"
    	set NoOfFiles to the number of items in item_list
    	if NoOfFiles = numberResponse then move entire contents of folder "Macintosh HD:Users:grace:Downloads:Regis:source" to "Macintosh HD:Users:grace:Desktop:destination"
    end tell
    Thank you
    :)
     
  2. kryten2, Sep 18, 2014
    Last edited: Sep 18, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    No coercion is done with equality and inequality tests so the comparison 5 = "5" is false. You need to coerce your numberResponse into an integer. You can do something like this :

    Code:
    set validNumber to false
    
    repeat while not validNumber
    	display dialog "How many render frames will you have?" default answer "Only Enter Integers"
    	try
    		set numberResponse to text returned of result [B]as integer[/B]
    		
    		set validNumber to true
    	end try
    	
    	if not validNumber then
    		display dialog "You didn't enter an integer" with icon caution
    	end if
    end repeat
    
    
    set renderFolder to choose folder with prompt "Please select directory."
    
    tell application "System Events"
    	set item_list to POSIX path of every disk item of renderFolder
    end tell
    
    tell application "Finder"
    	set NoOfFiles to the number of items in item_list
    	if NoOfFiles = numberResponse then move entire contents of folder "Macintosh HD:Users:grace:Downloads:Regis:source" to "Macintosh HD:Users:grace:Desktop:destination"
    end tell
    Also be aware that your item_list contains a hidden .DS_Store item which will mess up your NoOfFiles count.
    Use this to only list visible disk items :

    Code:
    tell application "System Events"
    	set item_list to POSIX path of every disk item of renderFolder [B]whose visible is true[/B]
    end tell
     
  3. registudio thread starter macrumors member

    registudio

    Joined:
    Aug 8, 2013
    Location:
    Montreal, QC
    #3

    Thank you for your help.
    Yep I noticed the .DS file that's why I've added in my last version +1 to numberResponse.
    I also started to play with delay for the cycling.

    Cheers
    Regis
     
  4. registudio thread starter macrumors member

    registudio

    Joined:
    Aug 8, 2013
    Location:
    Montreal, QC
    #4
    @kryten2
    All set and running smoothly with notification and email sent at the end.
    I did use my own code but yours was very very helpful to point me the mistakes I made.

    Thank you

    Cheers
    Regis
     

Share This Page