Jump to content
TingTingBen

Trouble with File filter and Run script

Recommended Posts

Hi,

 

I'm having trouble creating a workflow. Based on help found on macscripter.net, I wrote an AppleScript to change decimal and thousands separator within csv files.

tell application "Finder" to set FileToEdit to selection as string
set AccessToFile to open for access file FileToEdit with write permission
set CSVContent to read file FileToEdit

to switchText from t to r instead of s
	set d to text item delimiters
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to set t to item 1 & ({""} & rest)
	set text item delimiters to d
	t
end switchText

switchText from CSVContent to "." instead of ","
set FirstStepText to result
switchText from FirstStepText to "," instead of ";"
set TextToWrite to result

set eof of file FileToEdit to 0
write TextToWrite to AccessToFile
close access AccessToFile

The script works fine from the Finder selection. However, I would like to turn into an Alfred workflow, using a file filter.

To do so, the file filter searches for csv files, and I changed the first line of the script to:

set FileToEdit to POSIX file q

... or other variants, but nothing worked.

 

Do you have any idea to fix it? Thanks!

 

Share this post


Link to post

Hi,

 

I'm having trouble creating a workflow. Based on help found on macscripter.net, I wrote an AppleScript to change decimal and thousands separator within csv files.

tell application "Finder" to set FileToEdit to selection as string
set AccessToFile to open for access file FileToEdit with write permission
set CSVContent to read file FileToEdit

to switchText from t to r instead of s
	set d to text item delimiters
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to set t to item 1 & ({""} & rest)
	set text item delimiters to d
	t
end switchText

switchText from CSVContent to "." instead of ","
set FirstStepText to result
switchText from FirstStepText to "," instead of ";"
set TextToWrite to result

set eof of file FileToEdit to 0
write TextToWrite to AccessToFile
close access AccessToFile

The script works fine from the Finder selection. However, I would like to turn into an Alfred workflow, using a file filter.

To do so, the file filter searches for csv files, and I changed the first line of the script to:

set FileToEdit to POSIX file q

... or other variants, but nothing worked.

 

Do you have any idea to fix it? Thanks!

 

From a file filter, it would pass the file path and name to the next step (your script) in the {query} variable. So, try setting FileToEdit to "{query}". I think that should get you going

Share this post


Link to post

From a file filter, it would pass the file path and name to the next step (your script) in the {query} variable. So, try setting FileToEdit to "{query}". I think that should get you going

 

Ah, I got confused between q and {query}! So, the first line is

set FileToEdit to POSIX file "{query}"

... and it works perfectly. Thank you!

Share this post


Link to post

Ah, I got confused between q and {query}! So, the first line is

set FileToEdit to POSIX file "{query}"

... and it works perfectly. Thank you!

 

You're very welcome. Glad you got it sorted out :)

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...