Touch command to change selected file/folder time/date stamp

Hello,

The Automator workflow described at Add a simple ”Touch” Service to your context menu (Right-Click Menu) in MacOS X (Tested in Mac OS X 10.10.5, but should work in older and newer versions as well) with Automator.app · GitHub
is not functioning on my Sequoia 15.5 Mac. It does not update the selected Finder item (file or folder) to the current date/time when selecting the described workflow from the Services menu. .

The following script also is not modifying the selected file’s or folder’s date/time stamp. Nothing happens to the selected file or folder

How to fix either one?

on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else
			process_item(this_item)
		end if
	end repeat
end open

-- this sub-routine processes folders 
on process_folder(this_folder)
	do shell script "touch \"" & POSIX path of this_folder & "\""
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & ¬
			(item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else
			process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine processes files 
on process_item(this_item)
	do shell script "touch \"" & POSIX path of this_item & "\""
end process_item

I wrote this in 7 years ago. And works well with macOS 15.x.

http://piyocast.com/as/archives/4389

1 Like

bfh. I saved the script as an application on my desktop, and then dragged two folders and two files to the application icon. The dates of the folders and files were changed as expected. This is on my Sequoia computer. Without any error messages, it’s hard to know why the script isn’t working for your.

I modified the script to work on files and folders selected in a Finder window. You might run this from a script editor to see if it works and if not what the error messages are. I didn’t review the actual script because it seemed to work as expected.

BTW, this script changes the date/time stamp of files and folders located within a selected folder. This might not be immediately apparent and may not be desired behavior in all cases.

tell application "Finder" to set these_items to selection as alias list
if these_items is {} then display dialog "One or more files or folders must be selected in a Finder weindow" buttons {"OK"} cancel button 1 default button 1

repeat with i from 1 to the count of these_items
	set this_item to (item i of these_items)
	set the item_info to info for this_item
	if folder of the item_info is true then
		process_folder(this_item)
	else
		process_item(this_item)
	end if
end repeat

-- this sub-routine processes folders 
on process_folder(this_folder)
	do shell script "touch \"" & POSIX path of this_folder & "\""
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & ¬
			(item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else
			process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine processes files 
on process_item(this_item)
	do shell script "touch \"" & POSIX path of this_item & "\""
end process_item

Here is something to try.

Basically:

  • It gets the selected item and enters its path into the variable ‘path’ (for the second shell script)

  • If a folder, it then grabs the contents, including those of any subfolders

  • It then runs do shell script on the contents and subcontents

    • cycles through contents and subcontents
    • gets quoted path of all items and makes list of them
      NB does not include selected folder
    • coerces list into long, space-separated string for ‘touch
    • runs do shell script with ‘touch’ command to change ‘modified’ of listed files
  • It seems that you can’t casually get a selected folder and its contents simultaneously, so…

    • runs simpler do shell script for selected files/folders (collected in ‘path’ variable)

Actions

  • Service receives selected ‘files or folders’ in ‘Finder’;
  • Set value of variable ‘Path’;
  • Get folder contents; repeat for each subfolder checked;
  • Run applescript
    on run {input, parameters}
        set plist to {}
            repeat with i in input
                set qpp to quoted form of POSIX path of i
                set end of plist to qpp
            end repeat
	
        set AppleScript's text item delimiters to space
        set plist to (plist as text)
	
    	do shell script "/usr/bin/touch -m " & (plist as text)
    end run
  • Get value of variable ‘Path’;
  • Run applescript
   on run {input, parameters}
       set inp to quoted form of POSIX path of input
       do shell script "/usr/bin/touch -m " & inp
       return input
   end run

I haven’t tested it extensively as it’s a pain to do so.

However, it did work on three levels of folders and their contents as well as when two folders were selected.

I haven’t figured out how to combine the selected items with any folder contents yet but maybe someone else knows how to do so.

If you have taken an Automator workflow that was last saved on macOS 10.10.5 and tried to use it on Sequoia, you may need to open that Automator workflow on Sequoia in Automator and then resave it to update it.

I have seen several instances where this has bitten users until the old saved code is updated on the new system.

Many thanks for all the helpful replies.

My script has resumed functioning properly as mysteriously as it had stopped.

:thinking: