Jump to content
alfredclough

PHP Undefined Constant Error in Catalina

Recommended Posts

Every since I've upgraded to Catalina I have a workflow that is throwing an error but I cannot figure out why. It is a script folder that searches file in a folder. Here is the error:

 

[08:11:59.413] Logging Started...
[08:12:03.783] Recent Downloads[Script Filter] Queuing argument ''
[08:12:04.065] Recent Downloads[Script Filter] Script with argv '(null)' finished
[08:12:04.069] Recent Downloads[Script Filter] Warning: Use of undefined constant BASE_DIR - assumed 'BASE_DIR' (this will throw an Error in a future version of PHP) in Command line code on line 1

Warning: Use of undefined constant FILE_PATTERN - assumed 'FILE_PATTERN' (this will throw an Error in a future version of PHP) in Command line code on line 8

 

Here is the code for the script filter:

 

define(BASE_DIR, getenv('HOME') . '/Downloads');
$file_pattern = "{query}";
if ( $file_pattern ) {
	$file_pattern = trim($file_pattern);
	$file_pattern = preg_quote($file_pattern);
	$file_pattern = '/' . str_replace(' ', '.', $file_pattern) . '/i';
}
define(FILE_PATTERN, $file_pattern);
$dir_contents = scandir(BASE_DIR);
$files = array();
foreach ( $dir_contents as $file ) {
	$files[$file] = filemtime(BASE_DIR . '/' . $file);
}
arsort($files, SORT_NUMERIC);
//filter out non matching files
if ( $file_pattern ) {
	foreach ( $files as $file => $modtime ) {
		if ( ! preg_match(FILE_PATTERN, $file) ) {
			unset($files[$file]);
		}
	}
}
$items = new SimpleXMLElement("<items></items>");
if ( count($files) == 0 ) {
	$c = $items->addChild('item');
	$c->addAttribute('arg', 'No Match');
	$c->addAttribute('valid', 'no');
	$c->arg = 'No Match';
	$c->addAttribute('type', 'file');
	$c->title = 'No Match';
	$c->subtitle = '';
	$c->icon = 'icon.png';
	$c->icon->addAttribute('type', 'default');
}
foreach( $files as $basename => $modtime ) {
	$fullpath = BASE_DIR . '/' . $basename;
	if ( substr($basename, 0, 1) != '.' ) {
		$c = $items->addChild( 'item' );
		$c->addAttribute('arg', $fullpath);
		$c->arg = $fullpath;
		$c->addAttribute('type', 'file');
		$c->title = $basename;
		$c->subtitle = $fullpath;
		$c->icon = $fullpath;
		$c->icon->addAttribute('type', 'fileicon');
	}
}
echo $items->asXML();

 

I can't quite figure out what went wrong? I've used this for a long time however if there is an easier way to do this I'm also open for ideas.

Share this post


Link to post
6 hours ago, alfredclough said:

Every since I've upgraded to Catalina

 

It has nothing to do with macOS or Alfred.

 

Your PHP code is incorrect. You haven't used quotes in your defines, i.e. define(BASE_DIR, ...) should be define("BASE_DIR", ...)

 

Share this post


Link to post
Just now, alfredclough said:

I wonder why it worked for years

 

Because PHP was a very stupid language that is trying to be less stupid while not breaking things too much. Constant names and array keys without quotes have been deprecated for many years. I guess Catalina includes a newer version of PHP that finally makes that a real error (or it's configured to make it an error).

 

Share this post


Link to post

Thanks for this note - I know next to nothing about PHP but hopefully can fix the BibDesk workflow 

which has also died under Catalina.

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...